我对MVC-Chat Sample application做了一些简单的修改,并将它部署到运行在2个webworker-instances上的Azure。客户端只能与同一实例上的其他客户端通信,但稍后将使用背板解决此问题。
当我将新代码部署到服务器(来自visual studio)时,实例将逐个升级,所有客户端将首先重新连接到实例2(而实例1获取新代码),然后是所有客户端在升级实例2时将重新连接到实例1。 (我实际上并没有更改任何代码,只是重新部署)。
问题是:在客户端重新连接后,其中一些将无法接收发送给他们的消息,但他们将接收广播。
这是我的中心代码:
public class ChatHub : Hub
{
private string GetRoleId()
{
return RoleEnvironment.CurrentRoleInstance.Id;
}
public override Task OnConnected()
{
string message = String.Format("OnConnected ({0}) {1} - Version: {2}", GetRoleId(), Context.ConnectionId, System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
Clients.All.newMessage(message);
return base.OnConnected();
}
public override Task OnDisconnected()
{
Clients.All.newMessage(String.Format("OnDisconnected ({0}) {1} - Version: {2}", GetRoleId(), Context.ConnectionId, System.Reflection.Assembly.GetExecutingAssembly().GetName().Version));
return base.OnDisconnected();
}
public override Task OnReconnected()
{
Clients.All.newMessage(String.Format("OnReconnected ({0}) {1} - Version: {2}", GetRoleId(), Context.ConnectionId, System.Reflection.Assembly.GetExecutingAssembly().GetName().Version));
return base.OnReconnected();
}
public void SendToConnection(string connectionId, string message)
{
Clients.Client(connectionId).newMessage(message);
}
}
我的Index.cshtml文件也很简单,几乎没有修改:
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/jquery.signalR-1.1.3.js"></script>
<script src="~/signalr/hubs"></script>
<script type="text/javascript">
$(function () {
var addMessage = function(message) {
var time = new Date();
$('#info').prepend('<li>' + time.toUTCString() + ' - ' + message + '</li>');
};
var chat = $.connection.chatHub;
chat.client.newMessage = function (message) {
addMessage('Incomming message: ' + $.connection.hub.transport.name + ' - ' + message);
};
var start = function () {
$.connection.hub.logging = true;
addMessage('Before start');
$.connection.hub.start().done(function () {
addMessage('After start');
});
};
$.connection.hub.stateChanged(function (state) {
addMessage('State changed (' + state.newState + '). ID: <b>' + $.connection.hub.id + '</b></li>');
});
start();
});
</script>
<div id="info"></div>
要在浏览器中发送私信(我使用Chrome),请在开发者控制台中输入:
$.connection.chatHub.server.sendToConnection($.connection.hub.id, '<b>Hey!</b>')
&#34;嘿&#34;不会出现在大约5-15%的浏览器中。
这是在浏览器获取&#34; blinded&#34;重新连接到服务器:
[15:25:08 GMT+0200 (W. Europe Daylight Time)] SignalR: Keep alive has been missed, connection may be dead/slow. jquery.signalR-1.1.3.js:54
[15:25:14 GMT+0200 (W. Europe Daylight Time)] SignalR: Keep alive timed out. Notifying transport that connection has been lost. jquery.signalR-1.1.3.js:54
[15:25:16 GMT+0200 (W. Europe Daylight Time)] SignalR: Closing the Websocket jquery.signalR-1.1.3.js:54
[15:25:16 GMT+0200 (W. Europe Daylight Time)] SignalR: Clearing hub invocation callbacks with error: Connection started reconnecting before invocation result was received. jquery.signalR-1.1.3.js:54
[15:25:16 GMT+0200 (W. Europe Daylight Time)] SignalR: webSockets reconnecting jquery.signalR-1.1.3.js:54
[15:25:16 GMT+0200 (W. Europe Daylight Time)] SignalR: Connecting to websocket endpoint 'ws://myappname.cloudapp.net/signalr/reconnect?transport=webSockets&connectionToken=z-aUiXxW_jcBHDi631xCvrrQLoMtK0jVl3l-dRYcESU01cvkgDzwPfc8GlHk2P9zlmp-4GLwxLYdKzdUJi-vahn0qOGlEnc2Z85p8ecDT1fgTBLI0&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&messageId=B%2C14&tid=3' jquery.signalR-1.1.3.js:54
[15:25:25 GMT+0200 (W. Europe Daylight Time)] SignalR: Websocket opened jquery.signalR-1.1.3.js:54
[15:25:25 GMT+0200 (W. Europe Daylight Time)] SignalR: Triggering client hub event 'newMessage' on hub 'ChatHub'. jquery.signalR-1.1.3.js:54
[15:25:25 GMT+0200 (W. Europe Daylight Time)] SignalR: Triggering client hub event 'newMessage' on hub 'ChatHub'. jquery.signalR-1.1.3.js:54
> $.connection.chatHub.server.sendToConnection($.connection.hub.id, '<b>Hey!</b>')
Object {state: function, always: function, then: function, promise: function, pipe: function…}
任何有关解决此错误的帮助都将不胜感激。