【问题标题】:Client Application Name in DataSnapDataSnap 中的客户端应用程序名称
【发布时间】:2016-11-13 06:59:34
【问题描述】:

我有使用 DataSnap 的客户端-服务器系统。我想记录客户端应用程序数据,所以我使用 TDSServer - OnConnect 事件。在这种情况下,我可以使用以下代码访问我想要的内容:

IP:= DSConnectEventObject.ChannelInfo.ClientInfo.IpAddress  
ClientPort:= DSConnectEventObject.ChannelInfo.ClientInfo.ClientPort  
Protocol:= DSConnectEventObject.ChannelInfo.ClientInfo.Protocol  
AppName:= DSConnectEventObject.ChannelInfo.ClientInfo.AppName  

前 3 行没问题,但 AppName 为空!!!

(我在同一台计算机上运行服务器和客户端,即本地主机)

【问题讨论】:

    标签: delphi datasnap


    【解决方案1】:

    当客户端通过 TCP/IP 连接时,我无法找到任何有关如何指定 AppName 的在线信息。如果你看代码

    procedure TDSTCPChannel.Open;
    var
      ClientInfo: TDBXClientInfo;
    begin
      inherited;
      FreeAndNil(FChannelInfo);
      FChannelInfo := TDBXSocketChannelInfo.Create(IntPtr(FContext.Connection), FContext.Connection.Socket.Binding.PeerIP);
    
      ClientInfo := FChannelInfo.ClientInfo;
    
      ClientInfo.IpAddress := FContext.Connection.Socket.Binding.PeerIP;
      ClientInfo.ClientPort := IntToStr(FContext.Connection.Socket.Binding.PeerPort);
      ClientInfo.Protocol := 'tcp/ip';
    
      FChannelInfo.ClientInfo := ClientInfo;
    end;
    

    DataSnap.DSTCPServerTransport.Pas 中很明显没有设置 ClientInfo.AppName。

    但是,以下解决方法适用于西雅图演示 DataSnap Basic Server + Client:

    1. 在客户端中,将 Param 'AppName' 添加到 SqlConnection1 组件的 Params 和 将其值设置为“MyTestApp”。重新编译客户端。

    2. 在IDE中打开服务器,修改ServerContainerForm的代码如下图。

    代码:

    uses
       [...], DBXTransport;
    
    procedure TForm8.DSServer1Connect(DSConnectEventObject: TDSConnectEventObject);
    var
      S : String;  //  added
      Info : TDBXClientInfo;  //  added
    begin
       ActiveConnections.Insert;
       if DSConnectEventObject.ChannelInfo <> nil then
       begin
         ActiveConnections['ID'] := DSConnectEventObject.ChannelInfo.Id;
         ActiveConnections['Info'] := DSConnectEventObject.ChannelInfo.Info;
       end;
       ActiveConnections['UserName'] := DSConnectEventObject.ConnectProperties[TDBXPropertyNames.UserName];
       ActiveConnections['ServerConnection'] := DSConnectEventObject.ConnectProperties[TDBXPropertyNames.ServerConnection];
       ActiveConnections.Post;
    
       InsertEvent('Connect');
    
       //  following added to get AppName from client
    
       S := DSConnectEventObject.ConnectProperties['AppName'];
       Info := DSConnectEventObject.ChannelInfo.ClientInfo;
       Info.AppName := S;
    
       DSConnectEventObject.ChannelInfo.ClientInfo := Info;
       Caption :=  DSConnectEventObject.ChannelInfo.ClientInfo.AppName;
    end;
    

    如您所见,它通过在客户端的 AppName 中获取设置的值来工作 SqlConnection1.Params 调用 `DSConnectEventObject.ConnectProperties['AppName']' 然后显示在 ServerContainerForm 的 Caption 上。

    显然,您可以通过将任何其他名称/值对添加到客户端上的 SqlConnection 的参数中来传递任何其他名称/值对,然后通过调用 DSConnectEventObject.ConnectProperties[] 在服务器上获取它们。

    【讨论】:

    • 这是否允许您将 AppName 传递给服务器?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多