2016-06-22 133 views
0

我创建了一个SignalR集线器,并使其在我的Web应用程序中工作。现在我试图让一个单独的Windows服务向该集线器发送消息。根据我用于集线器连接URL的内容,我得到401未授权或SocketException。我错过了什么让Windows服务能够发送消息到集线器?从Windows服务访问Web应用程序中的SignalR Hub

启动类网络应用:

[assembly: OwinStartup(typeof(MmaWebClient.Startup))] 
namespace MmaWebClient 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.MapSignalR(); 
     } 
    } 
} 

集线器类网络应用:index.html中

[HubName("scannerHub")] 
public class ScannerHub : Hub 
{ 
    public void Send(string message) 
    { 
     Clients.All.broadcastMessage(message); 
    } 
} 

脚本引用:

<script src="../scripts/jquery-1.9.1.js"></script> 
<script src="../scripts/jquery.signalR-2.2.0.min.js"></script> 
<script src="../signalr/hubs"></script> 

的JavaScript(的作品)在我的AngularJS控制器:

if (!$scope.state.hub) { 
     $scope.state.hub = $.connection.scannerHub; 
     $scope.state.hub.client.broadcastMessage = function (message) { 
      onSubIdOrCassetteIdEntered(scanText); 
     } 
     $.connection.hub.start() 
      .done(function() { 
       console.log('Connected to SignalR. Connection ID: ' + $.connection.scannerHub.connection.id + 
        '. URL: ' + $.connection.scannerHub.connection.baseUrl +$.connection.scannerHub.connection.appRelativeUrl); 
      }) 
      .fail(function (response) { 
       showInfoModal.show('Connection to SignalR Failed', 'This connection is necessary to read the ' + 
        'scans from the scanner. Response message: ' + response.message); 
      }); 
    } 

最后,在Windows服务:

static void Main(string[] args) 
    { 
     AutoResetEvent scanReceivedEvent = new AutoResetEvent(false); 

     var connection = new HubConnection("http://localhost/MmaWebClient/signalr"); 
     //Make proxy to hub based on hub name on server 
     var myHub = connection.CreateHubProxy("scannerHub"); 
     //Start connection 

     connection.Start().ContinueWith(task => { 
      if (task.IsFaulted) { 
       Console.WriteLine("There was an error opening the connection:{0}", 
            task.Exception.GetBaseException()); 
      } else { 
       Console.WriteLine("Connected"); 
      } 

     }).Wait(); 


     myHub.On<string>("broadcastMessage", param => { 
      Console.WriteLine("Scan received from server = [{0}]", param); 
      scanReceivedEvent.Set(); 
     }); 

     string input = ""; 

     do 
     { 
      Console.WriteLine("Enter a value to send to hub or Q to quit."); 

      input = Console.ReadLine(); 

      if (input.ToUpperInvariant() != "Q") 
      { 
       myHub.Invoke<string>("Send", input); 
       scanReceivedEvent.WaitOne(1000); 
      } 

     } while (input.ToUpperInvariant() != "Q"); 

     connection.Stop(); 
    } 

当我创建在上面的代码中新的轮毂连接,我已经试过这三个URL没有成功:

var connection = new HubConnection("http://localhost/MmaWebClient/signalr"); 
var connection = new HubConnection("http://localhost:8080"); 
var connection = new HubConnection("http://localhost"); 

第一URL:401 Unauthorized
第二个网址:套接字例外
第三个网址:404未找到

+0

你尝试过'http:// localho st/MmaWebClient'?对于C#SignalR客户端,我使用的是没有signalr/hubs部分的url,它工作正常。 – Wojtek

+0

当我这样做时,我得到401未授权。 –

回答

2

我能够得到它通过设置连接上的凭据(第二行,下同)工作:

var connection = new HubConnection("http://localhost/MmaWebClient"); 
connection.Credentials = CredentialCache.DefaultNetworkCredentials; 
var myHub = connection.CreateHubProxy("scannerHub"); 
0

不是一个确定的答案,但是这里有一个映射SignalR端点的替代方法。很难得到这个URL的错误。

ASP.NET Startup.cs

public void Configuration(IAppBuilder app) 
{  
    app.Map("/foo", map => 
    { 
     //Specify according to your needs 
     var hubConfiguration = new HubConfiguration 
     { 
      EnableDetailedErrors = true 
     }; 
     map.RunSignalR(hubConfiguration); 
    }); 

    ConfigureAuth(app); 
} 

客户

var hubConnection = new HubConnection("http://localhost/foo", useDefaultUrl: false); 
var proxy = hubConnection.CreateHubProxy("scannerHub"); 
+0

我试过了,得到了404 Not Found。我不得不评论ConfigureAuth线路,因为它不适用于我。 –

+0

您是否使用自定义端口运行网页? – supertopi

相关问题