Delphi XE DataSnap.DSServer[2] TDSServer[2] BroadcastMessage-广播消息

Delphi XE DataSnap.DSServer[2] TDSServer[2] BroadcastMessage-广播消息

向所有客户端回调发送消息。

function TDSServer.BroadcastMessage(const ChannelName: string;
                                    const Msg: TJSONValue;
                                    const ArgType: Integer): Boolean;
var
  I, Count: Integer;
  CurrentPassed: Boolean;
  TunnelItem: TDSCallbackTunnel;
begin
  TMonitor.Enter(FCallbacks);
  try
    Count := FCallbacks.Count;
    if Count > 0 then
    begin
      Result := True;
      for I := Count - 1 downto 0 do
      begin
        try
          TunnelItem := FCallbacks.Items[I];
          CurrentPassed := True;
          if TunnelItem.ListeningOn(ChannelName) then
            CurrentPassed := TunnelItem.BroadcastMessage(Msg.Clone as TJSONValue, ChannelName,
                                                         CALLBACK_TIMEOUT, ArgType);
        except
          CurrentPassed := False;
          FCallbacks.Delete(I);
        end;
        Result := CurrentPassed and Result;
      end;
    end
    else
      Result := false;
  finally
    // Caller must pass clone because callee will free
    Msg.Free;
    TMonitor.Exit(FCallbacks);
  end;
end;

function TDSServer.BroadcastMessage(const ChannelName, CallbackId: string;
                                    const Msg: TJSONValue;
                                    const ArgType: Integer): Boolean;
var
  I, Count: Integer;
  TunnelItem: TDSCallbackTunnel;
begin
  TMonitor.Enter(FCallbacks);
  try
    Count := FCallbacks.Count;
    if Count > 0 then
    begin
      Result := True;
      for I := Count - 1 downto 0 do
      begin
        TunnelItem := FCallbacks.Items[I];
        if TunnelItem.ListeningOn(ChannelName) then
          try
            Result := TunnelItem.BroadcastMessage(CallbackId, Msg.Clone as TJSONValue) and Result;
          except
            Result := False;
            FCallbacks.Delete(I);
          end;
      end;
    end
    else
      Result := false;
    // Caller must pass clone of Msg because callee will free
    Msg.Free;
  finally
    TMonitor.Exit(FCallbacks);
  end;
end;

BroadcastMessage方法向所有客户端回调发送消息。BroadcastMessage有两个重载。使用ChannelName、Msg和ArgType参数调用第一个重载。

第二个重载向具有给定注册回调标识符的所有客户端回调发送消息。为此,调用中需要一个额外的CallbackId参数。

  • ChannelName  //表示通道回调的名称。
  • CallbackId //表示回调标识符的字符串。
  • Msg //包含要广播的消息的TJSONValue。
  • ArgType  //表示JSON参数。

 

 

 

创建时间:2022.11.04  更新时间:

posted on 2022-11-04 11:41  滔Roy  阅读(236)  评论(0编辑  收藏  举报

导航