我正在研究 SingalR( https://github.com/SignalR/SignalR )。
我真的很想向除发起事件的人之外的所有联系人发送消息。
例如,
在聊天应用程序中,有三个客户端(A、B、C)。
客户端 A 输入消息“Hello”并点击提交。
Clients.addMessage(数据);发送“Hello”给所有Cleint(包括cleint A)
我只想向客户端 B 和 C 发送“Hello”。
我怎样才能实现它?
// I think this can get all Clients, right?
var clients = Hub.GetClients<Chat>();
最佳答案
目前无法在服务器上过滤消息,但您可以阻止从客户端发送给调用者的消息。如果您查看 signalr 上的一些示例,您会发现它们在方法(通常称为 join)中为每个客户端分配一个生成的 id。每当您从集线器调用方法时,请传递调用客户端的 id,然后在客户端进行检查以确保客户端的 id 与调用者不同。例如
public class Chat : Hub {
public void Join() {
// Assign the caller and id
Caller.id = Guid.NewGuid().ToString();
}
public void DoSomething() {
// Pass the caller's id back to the client along with any extra data
Clients.doIt(Caller.id, "value");
}
}
客户端
var chat = $.connection.chat;
chat.doIt = function(id, value) {
if(chat.id === id) {
// The id is the same so do nothing
return;
}
// Otherwise do it!
alert(value);
};
希望有帮助。
关于asp.net - SignalR(Hub) 可以发送除信号生成器之外的消息吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8429460/