[英]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.