Can SignalR (Hub) send a message except to the signal maker?

I am learning SingalR ( https://github.com/SignalR/SignalR ).

I really want to send a message to all connections except the person who makes the event.

For example,

There are three clients in the Chat application (A, B, C).

Client A enter the message, "Hello" and clikc submit.

Clients.addMessage (data); send "Hello" All Cleint (enable cleint A)

I want to send "Hello" only to Client B and C.

How can I achieve this?

// I think this can get all Clients, right? var clients = Hub.GetClients<Chat>(); 
+7
source share
2 answers

It is not possible to filter the message on the server today, but you can block messages for the caller from the client side. If you look at some patterns in signalr, you will see that they assign each client a generated identifier for the client in a method (usually called a connection). Whenever you call a method from a hub, pass the identifier of the calling client, then on the client side, check to make sure that the client identifier does not match the identifier of the caller. eg.

 public class Chat : Hub { public void Join() { // Assign the caller and id Caller.id = Guid.NewGuid().ToString(); } public void DoSomething() { // Pass the caller id back to the client along with any extra data Clients.doIt(Caller.id, "value"); } } 

Client side

 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); }; 

Hope this helps.

+12
source

Now (v1.0.0) it is possible to use the Clients.Others property in the Hub .

For example: Clients.Others.addMessage(data) calls the addMessage method for all clients except the caller.

+5
source