/// <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> /// 服务器主动断开客户端连接 /// </summary> /// <param name="e"></param> public void CloseClientSocket(SocketAsyncEventArgs e, string msg, Exception ex = null) { var userToken = e.UserToken as IUserToken; if (userToken != null) { if (userToken.ConnectSocket != null) { if (userToken.ConnectSocket.Connected) { userToken.ConnectSocket.Shutdown(SocketShutdown.Both); userToken.ConnectSocket.Close(); } } // 减少连接到服务器的客户端的总数的计数器跟踪 if (_numConnectedSockets > 0) { Interlocked.Decrement(ref _numConnectedSockets); TaskHelper.Run(() => { OnUnAccepted?.BeginInvoke(_numConnectedSockets, userToken, null, null); }); } MyUserTokenPool.Push(userToken); UserTokenList.DelByUID(userToken); if (ex != null) { RaiseOnErrored(ex, userToken, msg); } } }
protected void SendFile(Message msg) { if (!UserTokenList.IsEmpty && (msg != null)) { var userToken = UserTokenList.GetUserTokenByUID(msg.Accepter); if (userToken != null) { ProcessSendMsg(userToken, msg); } } }
/// <summary> /// 发送频道消息 /// </summary> /// <param name="msg"></param> protected void SendChannelMsg(Message msg) { if (!UserTokenList.IsEmpty && (msg != null) && (msg.Data != null)) { var list = UserTokenList.GetListByChannelID(msg.Accepter); if (list.Count > 0) { Parallel.ForEach(list, userToken => { ProcessSendMsg(userToken, msg); }); } } }
/// <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)); } } }); } }