Esempio n. 1
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));
                        }
                    }
                });
            }
        }
Esempio n. 2
0
 private void _client_OnConnected(object sender)
 {
     _client.SendAsync(WSFrame.PackageClientHandShark(_serverIP, _serverPort));
 }
Esempio n. 3
0
 public void SendAsync(byte[] msg)
 {
     _client.SendAsync(WSFrame.PackageData(msg));
 }