1

我对MVC-Chat 示例应用程序进行了一些简单的修改,并将其部署到在 2 个 webworker 实例上运行的 Azure。客户端只能与同一实例上的其他客户端通信,但这将通过使用背板来解决。

当我将新代码部署到服务器(来自visual studio)时,实例将被一个一个升级,所有客户端将首先重新连接到实例2(而实例1获取新代码),然后所有客户端将重新连接到实例 1,而实例 2 已升级。(我实际上并没有更改任何代码,只是重新部署)。

问题是:客户端重新连接后,其中一些将无法接收发送给它们的消息,但它们会收到广播。

这是我的集线器代码:

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>')

“嘿”不会出现在大约 5-15% 的浏览器中。

这是当“失明”的浏览器重新连接到服务器时,javascript 控制台窗口上显示的内容:

[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…}

任何有关解决此错误的帮助将不胜感激。

4

0 回答 0