示例#1
0
 /// <summary>
 /// 传入数组字节
 /// </summary>
 /// <param name="buffer"></param>
 public override bool Send(byte[] buffer)
 {
     try
     {
         if (NetType == NetType.TcpType)
         {
             buffer = ConvertMsgToByte(buffer);
         }
         else
         {
             //websocket模式下  如果还没有握手成功则直接发送握手信息 不再打包
             if (WsShakeHand)
             {
                 buffer = WebSocketConverter.PackBuffer(buffer);
             }
         }
         return(Push(buffer));
     }
     catch (Exception e)
     {
         Console.WriteLine("UserToken Send buffer" + e.Message);
         Reset("UserToken Send buffer" + e.Message);
         return(false);
     }
 }
示例#2
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);
             }
         }
     }
 }
示例#3
0
 /// <summary>
 /// 发送字符串
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public override bool Send(string data)
 {
     try
     {
         byte[] buffer = null;
         if (NetType == NetType.TcpType)
         {
             buffer = ConvertMsgToByte(Encoding.UTF8.GetBytes(data));
         }
         else
         {
             buffer = WebSocketConverter.PackData(data);
         }
         return(Push(buffer));
     }
     catch (Exception e)
     {
         Console.WriteLine("UserToken Send " + e.Message);
         Reset("UserToken Send " + e.Message);
         return(false);
     }
 }