Exemple #1
0
 /// <summary>
 /// 异步执行握手请求
 /// </summary>
 /// <param name="client">客户端</param>
 /// <param name="path">请求路径</param>
 /// <returns></returns>
 public Task <SocketError> ExecuteAsync(WebSocketClient client, string path)
 {
     try
     {
         this.IsWaitting = true;
         this.taskSetter = new TaskSetter <SocketError>((s) => this.TrySetResult(SocketError.TimedOut)).TimeoutAfter(this.timeout);
         var handshakeBuffer = this.GenerateHandshakeBuffer(client, path, out this.secKey);
         client.Send(handshakeBuffer);
     }
     catch (SocketException ex)
     {
         this.TrySetResult(ex.SocketErrorCode);
     }
     catch (Exception)
     {
         this.TrySetResult(SocketError.SocketError);
     }
     return(this.taskSetter.Task);
 }
 /// <summary>
 /// 执行握手请求
 /// </summary>
 /// <param name="client">客户端</param>
 /// <param name="path">请求路径</param>
 /// <returns></returns>
 public SocketError Execute(WebSocketClient client, string path)
 {
     try
     {
         this.IsWaitting = true;
         this.taskSetter = new SyncTaskSetter <SocketError>();
         var handshakeBuffer = this.GenerateHandshakeBuffer(client, path, out this.secKey);
         client.Send(handshakeBuffer);
     }
     catch (SocketException ex)
     {
         this.TrySetResult(ex.SocketErrorCode);
     }
     catch (Exception)
     {
         this.TrySetResult(SocketError.SocketError);
     }
     return(((SyncTaskSetter <SocketError>) this.taskSetter).GetResult(this.timeout));
 }
        /// <summary>
        /// 异步执行握手请求
        /// </summary>
        /// <param name="client">客户端</param>
        /// <returns></returns>
        public Task <SocketError> ExecuteAsync(WebSocketClient client)
        {
            try
            {
                this.IsWaitting = true;
                this.taskSetter = new TaskSetter <SocketError>(this.timeout, () => this.TrySetResult(SocketError.TimedOut));

                var content         = this.GenerateHandshakeContent(client, out this.secKey);
                var handshakeBuffer = Encoding.ASCII.GetBytes(content);
                client.Send(handshakeBuffer);
            }
            catch (SocketException ex)
            {
                this.TrySetResult(ex.SocketErrorCode);
            }
            catch (Exception)
            {
                this.TrySetResult(SocketError.SocketError);
            }
            return(this.taskSetter.Task);
        }
Exemple #4
0
        /// <summary>
        /// 执行握手请求
        /// </summary>
        /// <param name="client">客户端</param>
        /// <param name="path">请求路径</param>
        /// <returns></returns>
        public SocketError Execute(WebSocketClient client, string path)
        {
            try
            {
                this.IsWaitting = true;
                this.taskSetter = new TaskSetter <SocketError>()
                                  .TimeoutAfter(this.timeout)
                                  .AfterTimeout((self) => self.SetException(new TimeoutException()));

                var handshakeBuffer = this.GenerateHandshakeBuffer(client, path, out this.secKey);
                client.Send(handshakeBuffer);
            }
            catch (SocketException ex)
            {
                this.TrySetResult(ex.SocketErrorCode);
            }
            catch (Exception)
            {
                this.TrySetResult(SocketError.SocketError);
            }
            return(this.taskSetter.GetResult());
        }
        /// <summary>
        /// 生成握手内容
        /// </summary>
        /// <param name="client">客户端</param>
        /// <param name="path">路径</param>
        /// <param name="secKey">安全Key</param>
        /// <returns></returns>
        private byte[] GenerateHandshakeBuffer(WebSocketClient client, string path, out string secKey)
        {
            var host = client.RemoteEndPoint.ToString();

            if (client.RemoteEndPoint is DnsEndPoint dnsEndpoint)
            {
                host = string.Format("{0}:{1}", dnsEndpoint.Host, dnsEndpoint.Port);
            }

            var keyBytes = SHA1.Create().ComputeHash(Guid.NewGuid().ToByteArray());

            secKey = Convert.ToBase64String(keyBytes);

            var header = HeaderBuilder.NewRequest(HttpMethod.GET, path);

            header.Add("Host", host);
            header.Add("Connection", "Upgrade");
            header.Add("Upgrade", "websocket");
            header.Add("Origin", "http://" + host);
            header.Add("Sec-WebSocket-Version", "13");
            header.Add("Sec-WebSocket-Key", this.secKey);
            return(header.ToByteArray());
        }
Exemple #6
0
 /// <summary>
 /// 执行握手请求
 /// </summary>
 /// <param name="client">客户端</param>
 /// <returns></returns>
 public SocketError Execute(WebSocketClient client)
 {
     return(this.ExecuteAsync(client).ConfigureAwait(false).GetAwaiter().GetResult());
 }