当前位置: 首页>>代码示例>>C#>>正文


C# WebSocket.Dispose方法代码示例

本文整理汇总了C#中System.Net.WebSockets.WebSocket.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# WebSocket.Dispose方法的具体用法?C# WebSocket.Dispose怎么用?C# WebSocket.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.WebSockets.WebSocket的用法示例。


在下文中一共展示了WebSocket.Dispose方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ProcessWebSocketRequestAsync

        internal async Task ProcessWebSocketRequestAsync(WebSocket webSocket, CancellationToken disconnectToken, Func<object, Task<WebSocketMessage>> messageRetriever, object state)
        {
            bool closedReceived = false;

            try
            {
                // first, set primitives and initialize the object
                WebSocket = webSocket;
                OnOpen();

                // dispatch incoming messages
                while (!disconnectToken.IsCancellationRequested && !closedReceived)
                {
                    WebSocketMessage incomingMessage = await messageRetriever(state).PreserveCulture();
                    switch (incomingMessage.MessageType)
                    {
                        case WebSocketMessageType.Binary:
                            OnMessage((byte[])incomingMessage.Data);
                            break;

                        case WebSocketMessageType.Text:
                            OnMessage((string)incomingMessage.Data);
                            break;

                        default:
                            closedReceived = true;

                            // If we received an incoming CLOSE message, we'll queue a CLOSE frame to be sent.
                            // We'll give the queued frame some amount of time to go out on the wire, and if a
                            // timeout occurs we'll give up and abort the connection.
                            await Task.WhenAny(CloseAsync(), Task.Delay(_closeTimeout)).PreserveCulture();
                            break;
                    }
                }

            }
            catch (OperationCanceledException ex)
            {
                // ex.CancellationToken never has the token that was actually cancelled
                if (!disconnectToken.IsCancellationRequested)
                {
                    Error = ex;
                    OnError();
                }
            }
            catch (ObjectDisposedException)
            {
                // If the websocket was disposed while we were reading then noop
            }
            catch (Exception ex)
            {
                if (IsFatalException(ex))
                {
                    Error = ex;
                    OnError();
                }
            }

            try
            {
#if CLIENT_NET45
                var webSocketState = GetWebSocketState(WebSocket);
                if (webSocketState == WebSocketState.Closed ||
                    webSocketState == WebSocketState.Aborted)
                {
                    // No-op if the socket is already closed or aborted
                }
                else
                {
                    // Initiate the WebSocket closing handshake. Only the client should ever do this.
                    await WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None).PreserveCulture();
                }
#endif
            }
            finally
            {
#if CLIENT_NET45
                WebSocket.Dispose();
#endif
                OnClose();
            }
        }
开发者ID:Choulla-Naresh8264,项目名称:SignalR,代码行数:82,代码来源:WebSocketHandler.cs

示例2: HandleSocket

 private async Task HandleSocket(WebSocket socket) {
     Guid key = Guid.NewGuid();
     BlockingCollection<object> collection = new BlockingCollection<object>();
     LogFacade.Instance.AddListener(key, entry => collection.Add(entry));
     try {
         bool took = true;
         await CheckHeaders(socket);
         while (socket.State == WebSocketState.Open && !CancellationToken.Token.IsCancellationRequested) {
             if (!took) {
                 await Task.Delay(1000);
                 await WriteSocket(socket, new { Ping = true });
             }
             object item;
             took = collection.TryTake(out item);
             if (took)
                 await WriteSocket(socket, item);
         }
     }
     catch (Exception ex) {
         LogFacade.Instance.LogException("Exception handling web socket connection", ex);
     }
     finally {
         this.GuardedExecution(() => {
             LogFacade.Instance.RemoveListener(key);
             socket.Dispose();
             LogFacade.Instance.LogInfo("Closed web socket connection");
         });
     }
 }
开发者ID:afgbeveridge,项目名称:Quorum,代码行数:29,代码来源:HttpEventListener.cs


注:本文中的System.Net.WebSockets.WebSocket.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。