我如何从服务器向WCF中的所有客户端发送通知消息(可以说广播)? [英] How can I send a notification message from server to all clients in WCF (broadcast you can say)?

查看:95
本文介绍了我如何从服务器向WCF中的所有客户端发送通知消息(可以说广播)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每秒从net tcp WCF服务向所有客户端发送通知消息, 广播可以说吗?

I want to send notification message every second from net tcp WCF service to all clients, Broadcast you can say?

有用的答案之后

我编写了以下方法,该方法将向所有连接的用户发送通知(心跳)

I wrote the following method that will send notifications (heartbeat) to all connected users

foreach (IHeartBeatCallback callback in subscribers)
{
  ThreadPool.QueueUserWorkItem(delegate(object state)
  {
    ICommunicationObject communicationCallback = (ICommunicationObject)callback;
    if (communicationCallback.State == CommunicationState.Opened)
    {
      try
      {
         callback.OnSendHeartBeat(_heartbeatInfo.message,    _heartbeatInfo.marketstart,_heartbeatInfo.marketend, _heartbeatInfo.isrunning,   DateTime.Now);
      }
      catch (CommunicationObjectAbortedException)
      {
        Logger.Log(LogType.Info, "BroadCast", "User aborted");
        communicationCallback.Abort();
      }
      catch (TimeoutException)
      {
       Logger.Log(LogType.Info, "BroadCast", "User timeout");
       communicationCallback.Abort();
      }
      catch (Exception ex)
      {
        Logger.Log(LogType.Error, "BroadCast", "Exception " + ex.Message + "\n" +  ex.StackTrace);
        communicationCallback.Abort();
      }

    }
    else
    {
      DeletionList.Add(callback);
    }
  }
  );
}

我担心调用回调方法,因为客户端可能会关闭其应用程序,但是我使用try catch处理了这个问题,减少了超时时间,并并行发送了广泛的类型转换,这样就足够了吗?

I am worried about calling the callback method as the client may close his application, but I handled it using the try catch, decrease the timeout, and send the broad cast in parallel, so is that sufficient?

推荐答案

您需要设置回调服务;我写了简单的初学者指南回来的时候

You'll need to setup a callback service; I wrote a simple beginners guide a while back

这篇关于我如何从服务器向WCF中的所有客户端发送通知消息(可以说广播)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆