예제 #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));
                        }
                    }
                });
            }
        }
예제 #2
0
파일: WSClient.cs 프로젝트: jsjxrj/GFF
 private void _client_OnConnected(object sender)
 {
     _client.SendAsync(WSFrame.PackageClientHandShark(_serverIP, _serverPort));
 }
예제 #3
0
파일: WSClient.cs 프로젝트: jsjxrj/GFF
 public void SendAsync(byte[] msg)
 {
     _client.SendAsync(WSFrame.PackageData(msg));
 }