2016-03-05 38 views
0

我在WPF SignalR服务器SignalR WPF服务器和ASP.NET MVC的问题

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启动客户端我得到 error

错误的客户端是http://localhost:8081在IIS快递

更新: 谈判请求中的错误

+0

什么是错误(HTTP状态码)? – Pawel

+0

@Pawel协商请求期间出错 – amplifier

回答

2

在这部分代码中

using (WebApp.Start(options)) 
{ 
} 

您创建并立即处理服务器对象。这意味着您在创建后立即关闭服务器。

您应该存储对该对象的引用,并在需要时调用处理。或者只是存储对象,让操作系统在进程退出时处理资源清理。

Documentation

相关部分:

返回值

类型:System.IDisposable的

的IDisposible实例,可以调用关闭Web应用程序。