我正在使用signalR 2.1.2,并向我的客户端推送通知。前端是负载平衡的。奇怪的是:有时有效,有时无效。它似乎与浏览器的类型无关。有时它在IE中工作,有时它在Chrome中工作。首先,我认为传输类型可能是问题所在,这就是为什么我将传输类型降级为长轮询,不幸的是,它没有帮助:
客户端:
$.connection.hub.logging = true; // Enable SignalR logging
$.connection.hub.start({ transport: 'longPolling' }).done(function() {
console.log("Connected, using transport method: " + $.connection.hub.transport.name);
}).fail(function() {
console.log("ERROR! Could not establish SignalR connection");
});
$.connection.hub.stateChanged(function(change) {
if (change.newState === $.signalR.connectionState.disconnected) {
console.log("State of SignalR connection changed to disconnected");
} else if (change.newState === $.signalR.connectionState.connected) {
console.log("State of SignalR connection changed to connected");
}
});
$.connection.MessageHub.client.newIncoming = function(id) {
// server call and that stuff
}
服务器端:
[HubName("MessageHub")]
public class MessageNotificationHub : Hub
{
// Methods that are implemented here, can be called by the client via ajax and broadcast to every other client
public override Task OnConnected()
{
return base.OnConnected();
}
}
服务器上:
public void Notify(long id)
{
var notifyHubContext = GlobalHost.ConnectionManager.GetHubContext<MessageNotificationHub>();
notifyHubContext.Clients.All.newIncoming(id);
}
没什么大不了的,只是看起来像另一个signalR样本。
以下是日志中的错误消息:
SignalR:客户端订阅了集线器"messagehub"。SignalR:正在与'/applicationname/signar/协商?clientProtocol=1.4&connectionData=%5B%7B%22名称%22%3A%22消息集线器%22%。。。
错误!无法建立SignalR连接
信号R:正在停止连接。
SignalR连接状态更改为断开
解码URI显示:https://server/applicationname/signalr/negotiate?clientProtocol=1.4&connectionData=[{"name":"messagehub"}]&_=1426756354570
负载平衡部分可能导致我所有的麻烦吗。
我感谢你的帮助!Thx
我发现问题出在web.config中缺少机器密钥。因此,web服务器A创建了另一个机器密钥,而不是web服务器B。SignalR使用机器密钥来验证消息是否来自有效的发件人。在我将机器密钥添加到web.config中,部署并关闭所有会话后,它就像魅力一样工作!