Exemplo n.º 1
0
 private void HandleMsg(List <byte> tmpBytes)
 {
     try
     {
         if (tmpBytes[0] == 1)   // 自定义消息
         {
             SocketMsg msg = new SocketMsg();
             msg.msgId = (tmpBytes[1] << 8) | tmpBytes[2];
             msg.msg   = Encoding.UTF8.GetString(tmpBytes.GetRange(3, tmpBytes.Count - 3).ToArray());
             pushMsg(msg);
         }
         else if (tmpBytes[0] == 2)   // 握手回调
         {
             string          tmpStr       = Encoding.UTF8.GetString(tmpBytes.GetRange(1, tmpBytes.Count - 1).ToArray());
             Proto_Handshake handshakeMsg = JsonUtility.FromJson <Proto_Handshake>(tmpStr);
             DealHandshake(handshakeMsg);
         }
         else if (tmpBytes[0] == 3)  // 心跳回调
         {
             if (heartbeatTimeoutTimer != null)
             {
                 heartbeatTimeoutTimer.Stop();
             }
         }
     }
     catch (Exception e1)
     {
         Debug.Log(e1);
         SocketClose();
     }
 }
Exemplo n.º 2
0
        private void DealHandshake(Proto_Handshake msg)
        {
            if (msg.heartbeat > 0)
            {
                heartbeatTimer          = new Timer();
                heartbeatTimer.Elapsed += SendHeartbeat;
                heartbeatTimer.Interval = msg.heartbeat * 1000;
                heartbeatTimer.Enabled  = true;

                heartbeatTimeoutTimer           = new Timer();
                heartbeatTimeoutTimer.Elapsed  += HeartbeatTimeout;
                heartbeatTimeoutTimer.AutoReset = false;
                heartbeatTimeoutTimer.Interval  = 4 * 1000;
            }
            route = new List <string>();
            for (int i = 0; i < msg.route.Length; i++)
            {
                route.Add(msg.route[i]);
            }
            SocketMsg openMsg = new SocketMsg();

            openMsg.msgId = (int)SocketOpenOrClose.open;
            pushMsg(openMsg);
        }