SignalR connectionID 是否特定于集线器?
Are SignalR connectionIDs hub-specific?
如果我有多个集线器,并将一个 JavaScript 客户端连接到所有集线器,它们之间的上下文 ConnectionID 是否相同?
有趣的问题。我不知道答案,所以我用 this example 稍微改变了一下来测试它。
枢纽 类:
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
是一样的。这就是我所期待的。试一试!
有道理,应该是用同一个连接发过来的。
如果我有多个集线器,并将一个 JavaScript 客户端连接到所有集线器,它们之间的上下文 ConnectionID 是否相同?
有趣的问题。我不知道答案,所以我用 this example 稍微改变了一下来测试它。
枢纽 类:
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
是一样的。这就是我所期待的。试一试!
有道理,应该是用同一个连接发过来的。