是SignalR connectionid特定于中心的



如果我有几个集线器,并将单个JavaScript客户端连接到所有这些集线器,它们之间的上下文ConnectionID是否相同?

有趣的问题。我不知道答案,所以我用这个例子来测试它,改变了一点。

Hub类:

public class ChatHub : Hub {
    public void Send(string name, string message) {
        string cid = Context.ConnectionId;
        Clients.All.sendMessage(name, message);
    }
}
public class ChatHub2 : Hub
{
    public void Send(string name, string message)
    {
        string cid = Context.ConnectionId;
        Clients.All.sendMessage(name, message);
    }
}

page.html连接到集线器:

var chat = $.connection.chatHub;
var chat2 = $.connection.chatHub2;
$.connection.hub.start().done(function () {
    // Call the Send method on the hub.
    chat.server.send('Me', 'Message to 1');
    chat2.server.send('Me', 'Message to 2');
});

我在Hub方法上设置了断点,并且两者都被调用,并且Context.ConnectionId是相同的。这正是我所期待的。试试吧!

这是有道理的,它应该使用相同的连接来发送消息。

最新更新