SignalR(Hub)可以发送信号发生器以外的信息吗?

Sto*_*pid 7 asp.net signalr

我正在学习SingalR(https://github.com/SignalR/SignalR).

我真的想向所有连接发送消息,除了发生事件的人.

例如,

在聊天应用程序中,有三个客户端(A,B,C).

客户端A键入消息"Hello"并提交clikc.

Clients.addMessage(数据); 发送"Hello"给All Cleint(包括提示A)

我想发送"Hello"只有客户B和C.

我怎样才能实现它?

// I think this can get all Clients, right?
var clients = Hub.GetClients<Chat>();
Run Code Online (Sandbox Code Playgroud)

dav*_*owl 12

今天无法在服务器上过滤消息,但您可以从客户端阻止消息给呼叫者.如果你看一下signalr上的一些样本,你会看到他们在一个方法(通常称为join)中为每个客户端分配一个生成的id给客户端.无论何时从集线器调用方法,传递调用客户端的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");
    }
}
Run Code Online (Sandbox Code Playgroud)

客户端

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);
};
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.


Osi*_*Osi 5

现在可以通过使用Clients.Others你的属性来实现(v1.0.0)Hub.

例如:在除调用者之外的所有客户端上Clients.Others.addMessage(data)调用该方法addMessage.