.NET Framework SignalR Clients.Caller 未按预期工作

发布于 2025-01-18 23:59:10 字数 1348 浏览 0 评论 0原文

我目前正在我的.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千秋岁 2025-01-25 23:59:10

您是否在 Program.cs 中添加配置?

using SignalRChat.Hubs;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddSignalR();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();
app.MapHub<ChatHub>("/chatHub");

app.Run();

你可以阅读这个参考:
微软

are you add configuration in Program.cs ?

using SignalRChat.Hubs;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddSignalR();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();
app.MapHub<ChatHub>("/chatHub");

app.Run();

and you can read this reference :
Microsoft

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文