SignalR groups: do not send messages to the sender

When I send a message to the signalR group that I subscribed to, I return a message!

I do not want this, I want him to send a message to everyone else in the group.

Is it possible? How?

+4
source share
2 answers

Now with SignalR you can use

Clients.OthersInGroup("foo").send(message); 

which does exactly what you need. It will send a SignalR client message to everyone in the group except the caller.

You can read more here: Wiki Contexts for SignalR

+5
source

Here you can send the ConnectionId to the client and verify it. For example, below is your hub:

 [HubName("moveShape")] public class MoveShapeHub : Hub { public void MoveShape(double x, double y) { Clients.shapeMoved(Context.ConnectionId, x, y); } } 

At the client level, you can do the following:

 var hub = $.connection.moveShape, $shape = $("#shape"), $clientCount = $("#clientCount"), body = window.document.body; $.extend(hub, { shapeMoved: function (cid, x, y) { if ($.connection.hub.id !== cid) { $shape.css({ left: (body.clientWidth - $shape.width()) * x, top: (body.clientHeight - $shape.height()) * y }); } } }); 

edit

Starting with SignalR 1.0.0-alpha, there is a built-in API for this if you use hubs:

 [HubName("moveShape")] public class MoveShapeHub : Hub { public void MoveShape(double x, double y) { Clients.Others.shapeMoved(x, y); } } 

This will broadcast the data to everyone except the subscriber.

+5
source

Source: https://habr.com/ru/post/1439833/


All Articles