コード例 #1
0
ファイル: MessageServer.cs プロジェクト: jsjxrj/GFF
 /// <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);
             });
         }
     }
 }
コード例 #2
0
        /// <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));
                        }
                    }
                });
            }
        }