当我向我订阅的signalR小组发送消息时,我收到了消息!
我不想要这个,我希望它将消息发送给小组中的其他人。
这可能吗?怎么样?
答案 0 :(得分:5)
您可以在此处执行的操作是将ConnectionId发送给客户端并进行检查。例如,下面是您的中心:
[HubName("moveShape")]
public class MoveShapeHub : Hub
{
public void MoveShape(double x, double y)
{
Clients.shapeMoved(Context.ConnectionId, x, y);
}
}
在客户端级别,您可以执行以下操作:
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
});
}
}
});
修改强>
从SignalR 1.0.0-alpha开始,如果您使用Hub,则有一个内置API:
[HubName("moveShape")]
public class MoveShapeHub : Hub
{
public void MoveShape(double x, double y)
{
Clients.Others.shapeMoved(x, y);
}
}
这将播放除了来电者以外的所有人的数据。
答案 1 :(得分:5)
现在使用SignalR,您可以使用
Clients.OthersInGroup("foo").send(message);
这正是你所追求的。它将向除呼叫者之外的组中的每个人发送SignalR客户端消息。
您可以在此处阅读更多内容:SignalR wiki Hubs