SignalR Send 方法在尝试轮询现有连接时未触发
SignalR Send method not firing while trying to poll for existing connections
我将最新版本的 SignalR 与 jQuery 一起使用,并在用户断开连接时出现一些奇怪的行为,但尽管已断开连接,但仍然在我的 "UserQueue" 中徘徊。
我认为这可能与页面刷新似乎几乎同时触发 OnDisconnected
和 OnConnected
事件有关。当我在这些方法之一中设置断点并单步执行时,我的指针在每一步中都在这两种方法之间来回跳动 (F10
)。
我想 运行 检查每个 OnConnected
事件,以确定谁实际连接到我的应用程序。我想在 OnConnected 事件中从我的 C# 代码触发一个 JS 事件,然后允许 client/front 端返回用户存在的确认:
public override Task OnConnected()
{
// Check for other users:
Clients.All.checkForOtherUsers();
// Do stuff with my user queue
var curmodelId = GetModelId(Context);
var curUserConnection = GetCurrentUser(Context);
// First create the ledger is missing, setting the modelId as the first key
if (_ledger == null)
{
_ledger = new ConnectionLedger(curmodelId, curUserConnection);
}
else
{
// key not found, create new connection pool this model
if (!_ledger.PoolIds.Contains(curmodelId))
{
_ledger.AddConnectionPool(curmodelId, curUserConnection);
}
else
{
var curModelPool = _ledger.ConnectionPools.First(x => x.Id == curmodelId);
curModelPool.UserQueue.Enqueue(curUserConnection);
}
}
return base.OnConnected();
}
现在在客户端JS中,如果我有这段代码:
modelHub.client.checkForOtherUsers = function () {
// I can see logging here if I add console.log("checking for other users...")
modelHub.server.send();
}
...我期待我的 C# Send
方法接收回上下文(如果用户实际出现在客户端以便 JS 执行)并更新我的 UserQueue
:
public void Send()
{
var curmodelId = GetModelId(Context);
var curModelPool = _ledger.ConnectionPools.First(x => x.Id == curmodelId);
var curUserConnection = GetCurrentUser(Context);
curModelPool.UserQueue.Enqueue(curUserConnection);
}
...但是我的 Send
方法从未被触发,即使我直接从 JS 控制台触发它也是如此 $.connection.modelingHub.server.send()
.
为什么 Send 方法不触发? SignalR 库的事件顺序/异步性质是否导致了这种情况?
也欢迎对整体设计发表评论(因为我怀疑它可能很愚蠢),但请注意,由于我自定义的身份验证配置,我无法访问 Context.User
或 Clients.User
。
好像有以下两个原因。
服务器端
在 OnConnected()
.
内调用 Clients.All.checkForOtherUsers();
不好
我建议在连接后调用它。
请参考
客户端
在调用启动方法之前可能需要注册 modelHub.client.checkForOtherUsers = function () {
。
Normally you register event handlers before calling the start method
to establish the connection. If you want to register some event
handlers after establishing the connection, you can do that, but you
must register at least one of your event handler(s) before calling the
start method.
- How to establish a connection
我将最新版本的 SignalR 与 jQuery 一起使用,并在用户断开连接时出现一些奇怪的行为,但尽管已断开连接,但仍然在我的 "UserQueue" 中徘徊。
我认为这可能与页面刷新似乎几乎同时触发 OnDisconnected
和 OnConnected
事件有关。当我在这些方法之一中设置断点并单步执行时,我的指针在每一步中都在这两种方法之间来回跳动 (F10
)。
我想 运行 检查每个 OnConnected
事件,以确定谁实际连接到我的应用程序。我想在 OnConnected 事件中从我的 C# 代码触发一个 JS 事件,然后允许 client/front 端返回用户存在的确认:
public override Task OnConnected()
{
// Check for other users:
Clients.All.checkForOtherUsers();
// Do stuff with my user queue
var curmodelId = GetModelId(Context);
var curUserConnection = GetCurrentUser(Context);
// First create the ledger is missing, setting the modelId as the first key
if (_ledger == null)
{
_ledger = new ConnectionLedger(curmodelId, curUserConnection);
}
else
{
// key not found, create new connection pool this model
if (!_ledger.PoolIds.Contains(curmodelId))
{
_ledger.AddConnectionPool(curmodelId, curUserConnection);
}
else
{
var curModelPool = _ledger.ConnectionPools.First(x => x.Id == curmodelId);
curModelPool.UserQueue.Enqueue(curUserConnection);
}
}
return base.OnConnected();
}
现在在客户端JS中,如果我有这段代码:
modelHub.client.checkForOtherUsers = function () {
// I can see logging here if I add console.log("checking for other users...")
modelHub.server.send();
}
...我期待我的 C# Send
方法接收回上下文(如果用户实际出现在客户端以便 JS 执行)并更新我的 UserQueue
:
public void Send()
{
var curmodelId = GetModelId(Context);
var curModelPool = _ledger.ConnectionPools.First(x => x.Id == curmodelId);
var curUserConnection = GetCurrentUser(Context);
curModelPool.UserQueue.Enqueue(curUserConnection);
}
...但是我的 Send
方法从未被触发,即使我直接从 JS 控制台触发它也是如此 $.connection.modelingHub.server.send()
.
为什么 Send 方法不触发? SignalR 库的事件顺序/异步性质是否导致了这种情况?
也欢迎对整体设计发表评论(因为我怀疑它可能很愚蠢),但请注意,由于我自定义的身份验证配置,我无法访问 Context.User
或 Clients.User
。
好像有以下两个原因。
服务器端
在 OnConnected()
.
内调用 Clients.All.checkForOtherUsers();
不好
我建议在连接后调用它。
请参考
客户端
在调用启动方法之前可能需要注册 modelHub.client.checkForOtherUsers = function () {
。
Normally you register event handlers before calling the start method to establish the connection. If you want to register some event handlers after establishing the connection, you can do that, but you must register at least one of your event handler(s) before calling the start method.
- How to establish a connection