2012-10-15 53 views

回答

5

现在与SignalR您可以使用

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

这确实你是什么之后。它将发送一个SignalR客户端消息给除呼叫者外的一个组中的每个人。

你可以在这里阅读更多:SignalR wiki Hubs

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-α ,如果您使用集线器,则有内置的API:

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

这将广播除主叫方以外的每个人的数据。

+1

这是SignalR v0.5.3的唯一途径。在下一个版本中,除了呼叫者或特定的ConnectionId以外,每个人都可以获得广播支持:https://github.com/SignalR/SignalR/issues/105 –

+0

@akoeplinger甜美!感谢分享! – tugberk

相关问题