[英]How to broadcast (push) a message send by one client, to all clients through a server using WCF NetHttpBinding (WebSockets)?
在.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);
}
我想做的是,當單個客戶端調用SayHelloDuplex()
方法時,將消息廣播給所有客戶端。 有沒有辦法訪問所有客戶端的回調通道? 或者我應該記錄所有客戶端的回調通道,以便以后在其他方法中使用(例如Connect()
)? 也許我是以錯誤的方式解決這個問題?
任何幫助將不勝感激。 謝謝
回調通道對於每個客戶端是唯一的,因此無法訪問所有客戶端的回調通道。
相反,您應該為列表中的每個客戶端保存回調通道,或者甚至更好地保存在字典中,以便您可以定位特定客戶端。
然后,當您想要向所有客戶端廣播消息時,只需查看列表即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.