private void ReceiveLoginUser(IWebSocketConnection connection, SocketRespons respons) { if (respons.Error == 0 && respons.Data != null) { UserConnection user = respons.Data?.ToObject <UserConnection>(); user.Connection = connection; user.ProcessId = respons.Pid; lock (_lock) { if (userConnections.ContainsKey(respons.Pid)) { userConnections[respons.Pid] = user; } else { userConnections.Add(respons.Pid, user); } } } List <ResponsHandlerCallBackDelegate> delegates; if (incomingResponsHandlers.TryGetValue(respons.Callback, out delegates)) { if (delegates != null && delegates.Count > 0) { delegates[0](respons); } } }
/// <summary> /// 发送数据 /// </summary> /// <param name="pid"></param> /// <param name="command"></param> /// <param name="data"></param> /// <returns></returns> private SocketRespons SendData(int pid, Command command, object data = null) { if (!userConnections.ContainsKey(pid)) { throw new Exception("当前微信Id不在线。"); } SocketRespons respons = null; string key = Guid.NewGuid().ToString(); JObject obj = null; if (data != null) { obj = QNSerialize.DeserializeObject <JObject>(QNSerialize.SerializeObject(data)); } else { obj = new JObject(); } obj.Add("Code", (int)command); obj.Add("Callback", key); var encode = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(obj.ToString())); userConnections[pid].Connection.Send(encode); AutoResetEvent returnWaitSignal = new AutoResetEvent(false); ResponsHandlerCallBackDelegate SendReceiveDelegate = (result) => { respons = result; returnWaitSignal.Set(); }; AppendIncomingPacketHandler(key, SendReceiveDelegate); if (!returnWaitSignal.WaitOne(30 * 1000))//三十秒超时 { RemoveIncomingPacketHandler(key, SendReceiveDelegate); throw new QNException(26, "请求超时。", null); } RemoveIncomingPacketHandler(key, SendReceiveDelegate); if (respons.Error != 0) { throw new QNException((uint)respons.Error, respons.Message); } return(respons); }
private void HandlerMessage(IWebSocketConnection connection, SocketRespons respons) { if (respons.Error != 0) { return; } if (respons.Data == null) { return; } switch (respons.Data["Type"].ToObject <int>()) { //todo:根据类型处理不同的消息 case 1: case 2: default: ReceiveOtherIMEvent?.Invoke(connection, respons.Data.ToObject <ReceiveOtherIMArgs>()); break; } }