在 SignalR 中向除 self 之外的所有客户端广播消息 [英] Broadcasting message to all clients except self in SignalR

查看:34
本文介绍了在 SignalR 中向除 self 之外的所有客户端广播消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这些问题很相似:

I realize that these questions are similar:

SignalR - 除呼叫者外的所有客户端广播

发送发送给 Group 中所有客户端的消息,当前客户端除外

但是,它们已经过时了,当前文档中的任何内容都没有给我任何关于我是否在做正确的事情的线索.

However, they are old and nothing in the current documentation gave me any clues as to whether I am doing the right thing or not.

这是问题/问题:

我需要能够从服务器端向所有连接的客户端广播一条消息,除了提交 http 请求的那个人 - 想象一下有人提交了一个表单,并让所有连接到同一个表单的人都被通知某事已被通知提交.

I need to be able to broadcast a message from the server-side to all connected clients except the guy that submitted the http request - imagine someone submitting a form and getting all people connected to the same form being notified that something has been submitted.

我现在的做法是这样的:

The way I am doing that now is something like this:

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
var connectionID = HttpContext.Request.Cookies["conn-id"].Value;
context.Clients.AllExcept(connectionID).addMessage(message);

每次建立连接时,我都会在客户端的 cookie 中设置 ConnectionID,如下所示:

And every time a connection is established, I set ConnectionID in a cookie on the client side as so:

$.connection.hub.start().done(function(){ 
      $.cookie("conn-id",$.connection.hub.id); 
});

所以我的问题是:

  1. 这是我在这种情况下可以采取的最佳/唯一方法吗?我可以读取从其他地方发出请求的客户端的连接 id,比如 hub.Context.ConnectionID 或类似于你从 HttpContext 获取 SessionID 的方式.当前.Session.SessionID?

  1. Is this the best/only approach I can take in this case? Can I read the connection id of the client that originated the request from somewhere else like hub.Context.ConnectionID or something similar to the way you would get the SessionID from HttpContext.Current.Session.SessionID?

例如,当客户端通过将其设置为 SessionID 进行连接时,我可以以编程方式设置连接 ID 吗?出于某种原因,这会导致问题吗?如果是这样,您能否解释一下如何以编程方式设置连接 ID 以及我需要连接到哪个事件才能执行此操作?

Can I set the connection id programmatically when a client connects by setting it to the SessionID, for example? Could that cause a problem for some reason? If so, can you please explain how to set the connection ID programmatically and to which event do I need to hook up in order to do this?

我注意到每次重新加载页面时,连接 ID 都会更改为不同的 guid.你如何只设置一次 ConnectionId 并至少在会话的生命周期内维护它?

I noticed that everytime the page is reloaded, the connection id is changed to a different guid. How do you set the ConnectionId only once and maintain it at least for the life of the Session?

推荐答案

您的方法确实有效,但正确的方法是使用内置 API:Clients.Others.addMessage(message).此外,无需在集线器本身内部获取集线器上下文.您可以随时通过 Context.ConnectionId 访问当前连接 ID 或通过 Clients.All.foo() 向客户端发送消息;

Your approach does work, however the correct approach is to use the built in API: Clients.Others.addMessage(message). Also there's no need to get the hub context inside the hub itself. You can always access the current connection ID via Context.ConnectionId or send message sto clients via Clients.All.foo();

并且您无法以编程方式设置 ConnectinoID.如果您想跨页面跟踪用户,请在您的服务器上维护一个用户列表,然后在他们浏览网站时重新分配连接 ID.

And you cannot set the ConnectinoID programatically. If you'd like to track users across pages maintain a list of users on your server and then just re-assign the connection id's as they navigate through the site.

这是一篇解释用户跟踪的帖子:SignalR 1.0 beta 连接工厂

Here's a post explaining the tracking of users: SignalR 1.0 beta connection factory

这篇关于在 SignalR 中向除 self 之外的所有客户端广播消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆