.NET Framework SignalR Clients.Caller 未按预期工作
我目前正在我的.NET Framework项目中使用SignalR,以将更新发送给客户端以进行长期运行。可以同时运行许多过程,客户将使用唯一的ID订阅该过程的任何一个过程。我正在使用小组来识别订阅特定过程的客户。如果客户端在中间订阅了一个过程,则必须将所有先前的消息发送给该客户端。代码类似于客户
public class ProgressHub : Hub
{
public async Task SubscribeToProgress(string id)
{
foreach (var message in GetPreviousMessages(id)) // Getting all the previous messages
{
await Clients.Caller.SendMessage(message); // Sending Messages to the current caller alone
}
await Groups.Add(Context.ConnectionId, id); // Added the current client to a group to be used further
}
}
,客户端听到发送消息
上面的代码段不起作用(网络选项卡中没有消息)。 我尝试了很多事情
等待client.client(context.connectionId).sendMessage(message);
等待客户端。 //仅检查它是否有效
没有等待
的所有上述内容,但似乎无效。
在摆弄了一点之后,我能够提出这一点
public class ProgressHub : Hub
{
public async Task SubscribeToProgress(string id)
{
await Groups.Add(Context.ConnectionId, id); // Adding client to the group first
foreach (var message in GetPreviousMessages(id))
{
await Clients.Group(id).SendMessage(message); // Sending messages to the group all together
}
}
}
,但这具有不良的副作用,将较旧的消息发送给已经连接的客户端。当然,我可以排除其他ConnectionID并发送消息,但这似乎是一个黑客。从逻辑上讲,第一个片段应该很好。
I am currently using SignalR in my .NET framework project to send updates to the client for a long running process. There can be many processes running simultaneously and the client will subscribe to any one of the process using an unique ID. I am using Groups to identify the clients who are subscribed to a particular process. If a client subscribes to a process in middle, I must send all the previous messages to that client. The code goes something like this
public class ProgressHub : Hub
{
public async Task SubscribeToProgress(string id)
{
foreach (var message in GetPreviousMessages(id)) // Getting all the previous messages
{
await Clients.Caller.SendMessage(message); // Sending Messages to the current caller alone
}
await Groups.Add(Context.ConnectionId, id); // Added the current client to a group to be used further
}
}
The client listens to Send Message
The above code snippet is not working (No messages in the network tab).
I tried many things
await Clients.Client(Context.ConnectionId).SendMessage(message);
await Clients.All.SendMessage(message); // Just to check if it works
all the above without await
, but nothing seems to work.
After fiddling around a bit, I was able to come up with this
public class ProgressHub : Hub
{
public async Task SubscribeToProgress(string id)
{
await Groups.Add(Context.ConnectionId, id); // Adding client to the group first
foreach (var message in GetPreviousMessages(id))
{
await Clients.Group(id).SendMessage(message); // Sending messages to the group all together
}
}
}
But this has an undesirable side effect of sending the older messages to client who are already connected. Sure, I can exclude the other connectionIDs and send out the message, but this seems like an hack. Logically speaking, the first snippet should have worked just fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在 Program.cs 中添加配置?
你可以阅读这个参考:
微软
are you add configuration in Program.cs ?
and you can read this reference :
Microsoft