SignalR 2 连接未保留
SignalR 2 connection not being persisted
我设置了示例 SignalR 中心 ChatHub,添加了连接列表。当它运行 OnConnected 时,我看到它被添加到列表中。但是,当我在另一个浏览器中打开该页面时(期望列表现在有 2 个连接,我在列表中看到 0 个连接)。 ChatHub 是否根据请求实例化?
List<string> connections = new List<string>();
public override Task OnConnected()
{
connections.Add(Context.ConnectionId);
return base.OnConnected();
}
是的,为每个请求创建一个集线器实例。
You don't instantiate the Hub class or call its methods from your own
code on the server; all that is done for you by the SignalR Hubs
pipeline. SignalR creates a new instance of your Hub class each time
it needs to handle a Hub operation such as when a client connects,
disconnects, or makes a method call to the server.
我设置了示例 SignalR 中心 ChatHub,添加了连接列表。当它运行 OnConnected 时,我看到它被添加到列表中。但是,当我在另一个浏览器中打开该页面时(期望列表现在有 2 个连接,我在列表中看到 0 个连接)。 ChatHub 是否根据请求实例化?
List<string> connections = new List<string>();
public override Task OnConnected()
{
connections.Add(Context.ConnectionId);
return base.OnConnected();
}
是的,为每个请求创建一个集线器实例。
You don't instantiate the Hub class or call its methods from your own code on the server; all that is done for you by the SignalR Hubs pipeline. SignalR creates a new instance of your Hub class each time it needs to handle a Hub operation such as when a client connects, disconnects, or makes a method call to the server.