SignalR wpf 服务器和 ASP.NET MVC 问题
SignalR wpf server and ASP.NET MVC issue
我在 WPF SignalR 服务器中有
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
Closing += (s, e) => ViewModelLocator.Cleanup();
StartOptions options = new StartOptions();
options.Urls.Add("http://localhost:8080");
using (WebApp.Start(options))
{
}
}
}
Hub.cs
[HubName("clientPushHub")]
public class TestHub : Hub
{
public string GetServerTime()
{
return DateTime.UtcNow.ToString();
}
public void Heartbeat()
{
Console.WriteLine("Hub Heartbeat\n");
Clients.All.heartbeat();
}
public Task JoinGroup(string groupId)
{
return Groups.Add(Context.ConnectionId, groupId);
}
public void addHit(string pageId)
{
}
public Task LeaveGroup(string groupId)
{
return Groups.Remove(Context.ConnectionId, groupId);
}
public override Task OnConnected()
{
return (base.OnConnected());
}
}
Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
PerformanceEngine performanceEngine = new PerformanceEngine(1000);
Task.Factory.StartNew(async () => await performanceEngine.OnPerformanceMonitor());
// map SignalR urls
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
EnableDetailedErrors = true
};
map.RunSignalR(hubConfiguration);
}
);
}
}
在我的 ASP.NET MVC 客户端中
Index.cshtml
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script type="text/javascript" src="~/signalr/hubs"></script>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/app.js"></script>
<body ng-app="signalRIntegrationApp">
<div ng-controller="ServerTimeController">
<div>
<h3>Time from server (being pushed every 5 seconds):</h3>
<h4>{{currentServerTime}}</h4>
</div>
</div>
</body>
并在 app.js
(function () {
var app = angular.module("signalRIntegrationApp", []);
app.service('signalRSvc', function ($rootScope) {
var proxy = null;
var initialize = function () {
var connection = $.hubConnection("http://localhost:8080");
proxy = connection.createHubProxy('clientPushHub');
connection.start(function () {
proxy.addHit();
});
proxy.on('serverTime', function (data) {
$rootScope.$emit('serverTime', data);
});
}
return {
initialize: initialize
};
});
app.controller('ServerTimeController', ["$scope", 'signalRSvc', function($scope, signalRSvc) {
var vm = this;
$scope.$on("serverTime", function (e, data) {
$scope.$apply(function () {
vm.currentServerTime = data;
});
});
signalRSvc.initialize();
}]);
})()
当我在 Firefox 中启动客户端时出现错误
客户端在 IIS Expresshttp://localhost:8081 上
更新:
协商请求期间出错
在这部分代码中
using (WebApp.Start(options))
{
}
您创建并立即处置服务器对象。这意味着您在创建后立即关闭服务器。
您应该存储对该对象的引用并在需要时调用处置。或者只存储对象并让 OS 在进程退出时处理资源清理。
相关部分:
Return Value
Type: System.IDisposable
An IDisposible instance that can be called to shut down the web app.
我在 WPF SignalR 服务器中有
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
Closing += (s, e) => ViewModelLocator.Cleanup();
StartOptions options = new StartOptions();
options.Urls.Add("http://localhost:8080");
using (WebApp.Start(options))
{
}
}
}
Hub.cs
[HubName("clientPushHub")]
public class TestHub : Hub
{
public string GetServerTime()
{
return DateTime.UtcNow.ToString();
}
public void Heartbeat()
{
Console.WriteLine("Hub Heartbeat\n");
Clients.All.heartbeat();
}
public Task JoinGroup(string groupId)
{
return Groups.Add(Context.ConnectionId, groupId);
}
public void addHit(string pageId)
{
}
public Task LeaveGroup(string groupId)
{
return Groups.Remove(Context.ConnectionId, groupId);
}
public override Task OnConnected()
{
return (base.OnConnected());
}
}
Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
PerformanceEngine performanceEngine = new PerformanceEngine(1000);
Task.Factory.StartNew(async () => await performanceEngine.OnPerformanceMonitor());
// map SignalR urls
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
EnableDetailedErrors = true
};
map.RunSignalR(hubConfiguration);
}
);
}
}
在我的 ASP.NET MVC 客户端中 Index.cshtml
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script type="text/javascript" src="~/signalr/hubs"></script>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/app.js"></script>
<body ng-app="signalRIntegrationApp">
<div ng-controller="ServerTimeController">
<div>
<h3>Time from server (being pushed every 5 seconds):</h3>
<h4>{{currentServerTime}}</h4>
</div>
</div>
</body>
并在 app.js
(function () {
var app = angular.module("signalRIntegrationApp", []);
app.service('signalRSvc', function ($rootScope) {
var proxy = null;
var initialize = function () {
var connection = $.hubConnection("http://localhost:8080");
proxy = connection.createHubProxy('clientPushHub');
connection.start(function () {
proxy.addHit();
});
proxy.on('serverTime', function (data) {
$rootScope.$emit('serverTime', data);
});
}
return {
initialize: initialize
};
});
app.controller('ServerTimeController', ["$scope", 'signalRSvc', function($scope, signalRSvc) {
var vm = this;
$scope.$on("serverTime", function (e, data) {
$scope.$apply(function () {
vm.currentServerTime = data;
});
});
signalRSvc.initialize();
}]);
})()
当我在 Firefox 中启动客户端时出现错误
客户端在 IIS Expresshttp://localhost:8081 上
更新: 协商请求期间出错
在这部分代码中
using (WebApp.Start(options))
{
}
您创建并立即处置服务器对象。这意味着您在创建后立即关闭服务器。
您应该存储对该对象的引用并在需要时调用处置。或者只存储对象并让 OS 在进程退出时处理资源清理。
相关部分:
Return Value
Type: System.IDisposable
An IDisposible instance that can be called to shut down the web app.