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

时间:2022-12-11 02:12:00

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. 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。我是否必须为每个连接创建一个组或开始使用集线器,或者有一种更简单的方法来按ID选择连接,例如:

GetConnectionById(id).Send(msg);

?

1 个解决方案

#1


2  

Message can be sent to a specific connection ID, but the syntax is not same as what you specified in the question. Following snippet shows the syntax:

消息可以发送到特定的连接ID,但语法与您在问题中指定的语法不同。以下代码段显示了语法:

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. It is possible to send message to a specific client by its ID if you use this version. Following snippet shows it:

我想,你知道SignalR 1 Alpha已经发布了。如果您使用此版本,则可以通过其ID向特定客户端发送消息。以下代码段显示:

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.

这里,((连接)连接).Identity给出请求客户端的连接ID。

#1


2  

Message can be sent to a specific connection ID, but the syntax is not same as what you specified in the question. Following snippet shows the syntax:

消息可以发送到特定的连接ID,但语法与您在问题中指定的语法不同。以下代码段显示了语法:

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. It is possible to send message to a specific client by its ID if you use this version. Following snippet shows it:

我想,你知道SignalR 1 Alpha已经发布了。如果您使用此版本,则可以通过其ID向特定客户端发送消息。以下代码段显示:

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.

这里,((连接)连接).Identity给出请求客户端的连接ID。