コード例 #1
0
        /// <summary>
        /// Disconnects the WebSocket connection.
        /// </summary>
        /// <param name="e">Disconect event arguments.</param>
        /// <returns></returns>
        public override async Task DisconnectAsync(SocketCloseEventArgs e)
        {
            //if (this.Socket.State != WebSocketState.Open || this.Token.IsCancellationRequested)
            if (close_requested)
            {
                return;
            }

            close_requested = true;
            try
            {
                await Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", Token).ConfigureAwait(false);

                e = e ?? new SocketCloseEventArgs(null)
                {
                    CloseCode = (int)WebSocketCloseStatus.NormalClosure, CloseMessage = ""
                };
                Socket.Abort();
                Socket.Dispose();
            }
            catch (Exception)
            { }
            finally
            {
                if (e == null)
                {
                    var cc = Socket.CloseStatus != null ? (int)Socket.CloseStatus.Value : -1;
                    e = new SocketCloseEventArgs(null)
                    {
                        CloseCode = cc, CloseMessage = Socket.CloseStatusDescription ?? "Unknown reason"
                    };
                }
                await OnDisconnectedAsync(e).ConfigureAwait(false);
            }
        }
コード例 #2
0
ファイル: WebSocketClient.cs プロジェクト: zhangbo27/spear
        public async Task ReceiveAsync(CancellationToken loopToken)
        {
            try
            {
                var stream = new MemoryStream();
                var buffer = System.Net.WebSockets.WebSocket.CreateServerBuffer(4096);
                while (Socket.State != WebSocketState.Closed && Socket.State != WebSocketState.Aborted && !loopToken.IsCancellationRequested)
                {
                    var result = await Socket.ReceiveAsync(buffer, loopToken);

                    if (loopToken.IsCancellationRequested)
                    {
                        continue;
                    }
                    if (Socket.State == WebSocketState.CloseReceived && result.MessageType == WebSocketMessageType.Close)
                    {
                        _logger.LogInformation($"Socket {SocketId}: Acknowledging Close frame received from client");
                        TaskCompletion.SetCanceled();
                        await Socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, result.CloseStatusDescription, CancellationToken.None);
                    }

                    if (Socket.State == WebSocketState.Open)
                    {
                        await stream.WriteAsync(buffer.Array, buffer.Offset, result.Count - buffer.Offset,
                                                loopToken);

                        stream.Seek(0, SeekOrigin.Begin);
                        if (!result.EndOfMessage)
                        {
                            continue;
                        }
                        OnReceive?.Invoke(stream.ToArray());
                        await stream.DisposeAsync();

                        stream = new MemoryStream();
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, $"Socket {SocketId}:");
            }
            finally
            {
                TaskCompletion.SetCanceled();

                _logger.LogInformation($"Socket {SocketId}: 停止服务,状态码 {Socket.State}");

                if (Socket.State != WebSocketState.Closed)
                {
                    Socket.Abort();
                }
                OnClose?.Invoke(SocketId, Socket);
                //if (Clients.TryRemove(SocketId, out _))
                Socket.Dispose();
                TaskCompletion.SetResult(true);
            }
        }
コード例 #3
0
ファイル: WebSocker.cs プロジェクト: KazooDotNet/TreeRouter
 public override void Abort()
 {
     Socket.Abort();
 }