/// <summary> /// 处理转发队列 /// </summary> private void ProcessReceivedMsg(IUserToken userToken, Message msg) { _receiveSemaphore.WaitOne(); try { if (msg != null) { switch (msg.Protocal) { case (byte)MessageProtocalEnum.Heart: msg.Accepter = msg.Sender; SendHeart(msg); break; case (byte)MessageProtocalEnum.Login: userToken.UID = msg.Accepter; UserTokenList.SetByUID(userToken); msg.Protocal = (byte)MessageProtocalEnum.RLogin; msg.Data = Encoding.UTF8.GetBytes("登录成功"); SendPrivateMsg(msg); break; case (byte)MessageProtocalEnum.Logout: userToken.UID = msg.Accepter; UserTokenList.DelByUID(userToken); break; case (byte)MessageProtocalEnum.Subscribe: userToken.UID = msg.Sender; UserTokenList.SetByChannelID(msg.Accepter, userToken); msg.Protocal = (byte)MessageProtocalEnum.RSubscribe; msg.Data = Encoding.UTF8.GetBytes("订阅成功"); SendPrivateMsg(msg); break; case (byte)MessageProtocalEnum.Unsubscribe: userToken.UID = msg.Accepter; UserTokenList.DelByChannelID(msg.Accepter, userToken); break; case (byte)MessageProtocalEnum.Message: SendChannelMsg(msg); break; case (byte)MessageProtocalEnum.PrivateMsg: SendPrivateMsg(msg); break; case (byte)MessageProtocalEnum.File: SendFile(msg); break; } } } catch (Exception ex) { RaiseOnErrored(ex, userToken, msg); } _receiveSemaphore.Release(); }
/// <summary> /// 服务器socket接收到数据 /// </summary> /// <param name="userToken"></param> /// <param name="args"></param> protected override void RaiseOnSocketReceived(IUserToken userToken, byte[] args) { var wsUserToken = (WSUserToken)userToken; if (!wsUserToken.HandShaked) { string msg = Encoding.UTF8.GetString(args, 0, args.Length); if (msg.Contains("Sec-WebSocket-Key")) { var data = WSFrame.PackageHandShakeData(args, args.Length); wsUserToken.HandShaked = true; base.SendAsync(wsUserToken, data); wsUserToken.UID = Guid.NewGuid().ToString("N"); wsUserToken.ReceiveArgs.UserToken = wsUserToken; UserTokenList.SetByChannelID(channelID, wsUserToken); OnConnected?.BeginInvoke(_numConnectedSockets, wsUserToken, null, null); } } else { userToken.UnPackage(args, (bytes) => { OnSocketReceived?.BeginInvoke(userToken, bytes, null, null); var list = UserTokenList.GetListByChannelID(channelID); if (list != null) { foreach (var item in list) { base.SendAsync(item, WSFrame.PackageData(bytes)); } } }); } }