ree*_*erz 5 c# wcf websocket windows-8
在 .NET 4.5 中,引入了新的 WCF 绑定 NetHttpBinding,它使用 WebSocket 协议作为其底层传输。这意味着这可以实现来自服务器的真正推送。现在,我已经能够使用回调合约进行某种推送,如下所示:
[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);
}
Run Code Online (Sandbox Code Playgroud)
不过,我想做的是,当单个客户端调用该方法时,将消息广播给所有客户端SayHelloDuplex()。有没有办法访问所有客户端的回调通道?或者我应该记录所有客户端的回调通道以供以后在其他方法中使用(例如Connect())?也许我以错误的方式解决这个问题?
任何帮助将不胜感激。谢谢
每个客户端的回调通道都是唯一的,因此无法访问所有客户端的回调通道。
相反,您应该将每个客户端的回调通道保存在列表中,甚至更好地保存在字典中,以便您可以定位特定客户端。
然后,当您想向所有客户端广播消息时,只需查看列表即可。