向除SignalR中自身以外的所有客户端广播消息 [英] Broadcasting message to all clients except self in SignalR
问题描述
我意识到这些问题很相似:
I realize that these questions are similar:
但是,它们太旧了,在当前文档中,没有任何关于我是否在做正确的事情的线索。
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);
});
所以我的问题是:
-
在这种情况下,这是最好/唯一的方法吗?我可以从
hub.Context.ConnectionID
之类的其他地方读取发起请求的客户端的连接ID还是类似于获取SessionID
来自HttpContext.Current.Session.SessionID?
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 theSessionID
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,并至少在Session的整个生命周期内维护它?
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中自身以外的所有客户端广播消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!