如何从服务器向 WCF 中的所有客户端发送通知消息(可以说是广播)?

发布于 2024-07-24 10:34:23 字数 1237 浏览 5 评论 0原文

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

在提供有用的答案之后,

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

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 want to send notification message every second from net tcp WCF service to all clients,
Broadcast you can say?

After the helpful answers

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);
    }
  }
  );
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-07-31 10:34:23

您需要设置回拨服务; 我写了一个简单的初学者指南一段时间以前

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

七婞 2024-07-31 10:34:23

为此,您需要创建并维护所有已连接客户端的列表(为此的一般做法是创建 LogIn 和 LogOut 方法来创建和管理代表客户端的对象列表,包括其 CallbackContext)。
然后,使用 System.Time.Timers,您可以循环连接的客户端列表并发送通知。

提示。 此方法还可以充当 Keep-Alive 或 Hear-Beat 方法(如果这不是其设计目的),如果服务无法向客户端发送回调,则添加从列表中删除客户端的可能性。

In order to do that, you need to create and mantain a list of all connected clients (the general practice to fo this is creating LogIn and LogOut methods to create and manage a list of object representing your clients incuding their CallbackContext).
Then, with a System.Time.Timers, you can loop through the connected client list and send the notification.

Tip. this method could also act as a Keep-Alive or Hear-Beat method (if this isn't it's purpose by design) by adding the possiblity to remove clients from your list if the service cannot send the callback to them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文