Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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(). 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 (E.g. Connect())? Perhaps I'm tackling this problem in the wrong way?

Any help will be appreciated. Thanks

like image 965
reederz Avatar asked Oct 06 '22 01:10

reederz


1 Answers

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.

like image 133
yakiro Avatar answered Oct 10 '22 01:10

yakiro