简体   繁体   English

如何通过ID发送消息到signalR特定的PersistentConnection? (使用asp.net mvc4 web-api)

[英]How to send message to signalR specific PersistentConnection by its ID ? (with asp.net mvc4 web-api)

I use class Echo derived from PersistentConnection , and I want to send a message to certain connection: 我使用从PersistentConnection派生的类Echo ,我想向某个连接发送消息:

var client = GlobalHost.ConnectionManager.GetConnectionContext<Echo>();
client.Connection.Broadcast(msg);

But I want to send it to specific connection ID. 但我想将其发送到特定的连接ID。 Do I have to create a group for every connection or start using Hubs, or there is a simpler way to select connection by ID, something like: 我是否必须为每个连接创建一个组或开始使用集线器,或者有一种更简单的方法来按ID选择连接,例如:

GetConnectionById(id).Send(msg);

?

Message can be sent to a specific connection ID, but the syntax is not same as what you specified in the question. 可以将消息发送到特定的连接ID,但语法与您在问题中指定的语法不同。 Following snippet shows the syntax: 以下代码段显示了语法:

return Connection.Send(connectionId, Message);

Source of my answer: SignalR wiki on Github 我的答案来源: Github上的SignalR wiki

I think, you are aware that SignalR 1 Alpha is released. 我想,你知道SignalR 1 Alpha已经发布了。 It is possible to send message to a specific client by its ID if you use this version. 如果您使用此版本,则可以通过其ID向特定客户端发送消息。 Following snippet shows it: 以下代码段显示:

var connection = GlobalHost.ConnectionManager.GetConnectionContext<Echo>().Connection;
connection.Send(((Connection)connection).Identity, "Message to be sent");

Here, ((Connection)connection).Identity gives connection ID of the requesting client. 这里, ((Connection)connection).Identity给出请求客户端的连接ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM