Пример #1
0
 public void InvokeMessage()
 {
     while (true)
     {
         Thread.Sleep(1);
         ReciveData data = null;
         try
         {
             if (MQ.TryDequeue(out data))
             {
                 if (!data.UserToken.WsShakeHand)
                 {
                     var shakingKey = WebSocketConverter.GetSecKeyAccetp(data.Buffer, data.Actual);
                     if (!string.IsNullOrEmpty(shakingKey))
                     {
                         var buffer = WebSocketConverter.PackHandShakeData(shakingKey);
                         if (data.UserToken.Send(buffer))
                         {
                             data.UserToken.WsShakeHand = true;
                             Console.WriteLine("已经发送握手协议了....");
                         }
                     }
                     else
                     {
                         data.UserToken.Reset("无效的协议");
                     }
                 }
                 else
                 {
                     if (string.IsNullOrEmpty(data.UserToken.SessionId))
                     {
                         data.UserToken.SessionId = Guid.NewGuid().ToString("N");
                         ISessionAdd.AddSession(data.UserToken);
                         ISessionAdd.OnConnected(data.UserToken);
                     }
                     var(message, buffer) = WebSocketConverter.AnalyticData(data.Buffer, data.Actual);
                     data.Message         = message;
                     data.Buffer          = buffer;
                     data.Actual          = buffer.Length;
                     Process.Invoke(data);
                 }
             }
         }
         catch (Exception e)
         {
             if (data != null)
             {
                 data.UserToken.Reset("WsProcessor " + e.Message);
             }
         }
     }
 }
Пример #2
0
 /// <summary>
 /// 信息处理
 /// </summary>
 /// <param name="server"></param>
 public void InvokeMessage()
 {
     while (true)
     {
         ReciveData data = null;
         try
         {
             if (MCQ.TryDequeue(out data))
             {
                 //登陆保存会话 此处已经分配了SESSIONID
                 if (data.Command == Command.Login && ISessionAdd != null)
                 {
                     ISessionAdd.AddSession(data.UserToken);
                     data.UserToken.LoginFinish();
                     ISessionAdd.OnConnected(data.UserToken);
                 }
                 else if (data.Command == Command.Token)
                 {
                     data.UserToken.LastTokenDateTime = DateTime.Now;
                 }
                 else
                 {
                     //客户端定时发送心跳
                     data.UserToken.Token(data);
                     Process.Invoke(data);
                 }
             }
             else
             {
                 Thread.Sleep(1);
             }
         }
         catch (Exception e)
         {
             if (data != null)
             {
                 data.UserToken.Reset("InvokeMessage:" + e.Message + e.StackTrace);
             }
         }
     }
 }