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

问题描述 投票:7回答:2

我正在研究SignalR(qazxsw poi)。

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

例如,

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

客户端A键入消息“Hello”并单击“提交”。

Clients.addMessage(数据);向所有客户端发送“Hello”(包括客户端A)

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

我怎样才能实现它?

https://github.com/SignalR/SignalR
asp.net signalr
2个回答
12
投票

今天无法在服务器上过滤消息,但您可以从客户端阻止消息给呼叫者。如果你看一下signalr上的一些样本,你会看到他们在一个方法(通常称为join)中为每个客户端分配一个生成的id给客户端。无论何时从集线器调用方法,传递调用客户端的id,然后在客户端进行检查以确保客户端的id与调用者的ID不同。例如

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

客户端

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

希望有所帮助。


5
投票

现在(v1.0.0)可以通过在你的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); }; 中使用Clients.Others属性来实现。

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

© www.soinside.com 2019 - 2024. All rights reserved.