我創建了一個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未找到
你嘗試過'http:// localho st/MmaWebClient'?對於C#SignalR客戶端,我使用的是沒有signalr/hubs部分的url,它工作正常。 – Wojtek
當我這樣做時,我得到401未授權。 –