コード例 #1
0
        protected virtual void OnTcpServer_MessageReceived(object sender, TcpRawMessageReceivedEventArgs args)
        {
            GeneralMessage clientMessage = GeneralMessage.Deserialize(args.Message.MessageRawData, false);

            //if the server message is model
            if (clientMessage.Header == (int)MessageHeader.Model)
            {
                //get model from general message
                object model = _tcpServer.ConvertToModel(clientMessage);
                if (model is QueryServerTimeRequest)
                {
                    QueryServerTimeRequest  queryTimeReq = model as QueryServerTimeRequest;
                    QueryServerTimeResponse response     = new QueryServerTimeResponse
                    {
                        RequestID  = queryTimeReq.ID,
                        ServerTime = DateTime.Now
                    };
                    ServerRecvText += $"Received Request:\r\nfrom {queryTimeReq.Name}\r\n";
                    _tcpServer.SendModelMessage(args.Message.ClientID, response);
                }
                else
                {
                    //do something for other model
                }
            }
            else
            {
            }
        }
コード例 #2
0
 public override void ClientSend()
 {
     if (!string.IsNullOrWhiteSpace(ClientSendText))
     {
         //_tcpClient.SendText(ClientSendText.Trim());
         QueryServerTimeRequest request = new QueryServerTimeRequest()
         {
             Name    = "jackson",
             Message = ClientSendText
         };
         QueryServerTimeResponse response = _tcpClient.QueryAsync <QueryServerTimeResponse>(request).Result;
         ClientRecvText += "server time: " + response.ServerTime.ToString() + "\r\n";
     }
 }
コード例 #3
0
 protected virtual void OnTcpCient_ClientStatusChanged(object sender, ClientStatusChangedEventArgs args)
 {
     ClientStatus = args.Status;
     if (args.Status == ClientStatus.Closed)
     {
         CanConnect = true;
     }
     if (args.Status == ClientStatus.Connected)
     {
         QueryServerTimeRequest request = new QueryServerTimeRequest()
         {
             Name    = "jackson",
             Message = ClientSendText
         };
         QueryServerTimeResponse response = _tcpClient.QueryAsync <QueryServerTimeResponse>(request).Result;
         ClientRecvText += "server time: " + response.ServerTime.ToString() + "\r\n";
     }
 }