简体   繁体   English

如何通过使用WCF NetHttpBinding(WebSockets)的服务器将一个客户端发送的消息广播(推送)到所有客户端?

[英]How to broadcast (push) a message send by one client, to all clients through a server using WCF NetHttpBinding (WebSockets)?

In .NET 4.5 a new WCF binding- NetHttpBinding- has been introduced which uses WebSocket protocol as it's underlying transport. 在.NET 4.5中,引入了一个新的WCF绑定-NetHttpBinding-,它使用WebSocket协议作为底层传输。 Which implies that this enables a true push from server. 这意味着这可以实现从服务器的真正推送。 Now, I have been able to make some sort of push using A callback contract like this: 现在,我已经能够使用这样的回调契约进行某种推动:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class WebSocketSampleService : IDuplexContract
{
    public string SayHelloDuplex()
    {
        //push to the current caller
        OperationContext.Current.
            GetCallbackChannel<IDuplexCallbackContract>().
            SayingHello("Hello from WebSockets");

        //answer the current caller in the regular http way
        return "Hello";
    }
}
[ServiceContract(CallbackContract=typeof(IDuplexCallbackContract))]
public interface IDuplexContract
{
    [OperationContract]
    string SayHelloDuplex(string name);
}
[ServiceContract]
public interface IDuplexCallbackContract
{
    [OperationContract]
    void SayingHello(string message);
}

What I would like to do though, is to broadcast the message to all clients when a single client calls the method SayHelloDuplex() . 我想做的是,当单个客户端调用SayHelloDuplex()方法时,将消息广播给所有客户端。 Is there a way to access the callback channels of all clients? 有没有办法访问所有客户端的回调通道? Or should I record the callback channels of all the clients for later use in some other method (Eg Connect() )? 或者我应该记录所有客户端的回调通道,以便以后在其他方法中使用(例如Connect() )? Perhaps I'm tackling this problem in the wrong way? 也许我是以错误的方式解决这个问题?

Any help will be appreciated. 任何帮助将不胜感激。 Thanks 谢谢

Callback channel is unique per client, so there is no way to access the callback channel of all clients. 回调通道对于每个客户端是唯一的,因此无法访问所有客户端的回调通道。

Instead you should save the callback channel for each client in a list or even better in a dictionary so you can target specific client. 相反,您应该为列表中的每个客户端保存回调通道,或者甚至更好地保存在字典中,以便您可以定位特定客户端。

Then when you want to broadcast a message to all clients just go over the list. 然后,当您想要向所有客户端广播消息时,只需查看列表即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WCF双工回调,如何向所有客户端发送消息? - WCF duplex callbacks, how do I send a message to all clients? 向所有连接的客户端发送广播 POP-UP 消息 - Send a broadcast POP-UP Message to All the Connected Clients 节俭:向连接到服务器的所有客户端发送消息 - Thrift: Send a message to all clients connected to the server 从服务器向所有客户端发送信号器消息 - Send signalr message from server to all clients 如何在服务器广播中使用 signalr 仅对单个客户端,而不是所有客户端? - How can I use signalr in server broadcast to a single client only, not all clients? 如何使用grpc通过服务器在客户端之间发送消息? - how to send messages between clients through server using grpc? 如何通过Web API向所有客户端广播消息 - How do i broadcast a message to all clients via Web API WCF - 使用NetHttpBinding(WebSockets)或备用双工绑定发送JSON - WCF - Sending JSON with NetHttpBinding(WebSockets) or alternative Duplex Binding 客户端的WCF服务器/客户端数量 - WCF server/client count of clients 如何从服务器向客户端发送已连接客户端的列表,以区分常规消息? - How do I send a list of connected clients from the server to the client, differentiating between a regular message?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM