更新时间:2023-02-13 21:47:37
Signalr不在监视数据库中的更改.因此,当您仅在数据库中将用户设置为非活动状态时,这对Signalr毫无意义.您的3个客户端仍处于连接状态.
Signalr is not watching your database for changes. So when you just set the user to inactive in the database, it means nothing to Signalr. Your 3 clients are still connected.
要获得理想的结果,请在集线器中添加类似的内容
To get the desired result add something like this to your Hub
public override OnConnected()
{
// Increase the active user count in the db
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ServerHub>();
Clients.All.broadcastCount(DB.GetCount());
return base.OnConnected();
}
public override OnDisconnected()
{
//Decrease the connected user count in the db
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ServerHub>();
Clients.All.broadcastCount(DB.GetCount());
return base.OnDisconnected();
}
然后,当您连接和断开客户端连接时,集线器将通知连接的客户端.
Then when you connect and disconnect your clients, the hub will notify connected clients.
您将需要以SignalR捕获的方式断开连接,因此您不仅可以更改数据库中的标志.尝试从客户端调用 $.connection.hub.stop();
.
You will need to disconnect in a way that SignalR will catch, so you can't just change a flag in the database. Try calling $.connection.hub.stop();
from your client.
此链接对此有更详细的说明.
This link goes into more detail on it.
如果您说在数据库中更新后触发了 dependency_OnChange
事件,则不要调用 SendNotifications();
,而是调用中心方法,特别是NotifyAllClients(...)
If you say the dependency_OnChange
event is fired after you update in the database, then Instead of calling SendNotifications();
, call a hub method, specifically NotifyAllClients(...)