Exemple #1
0
 private void OnClosed(object sender, WebSocketConnectionEventArgs e)
 {
     if (e.Url == this.url)
     {
         this.Closed?.Invoke(this, e);
     }
 }
Exemple #2
0
        private void OnOpened(object sender, WebSocketConnectionEventArgs e)
        {
            if (e.Url == this.url)
            {
                e.IsHandled = true;

                this.Opened?.Invoke(this, e);
            }
        }
Exemple #3
0
 private void OnClosed(object sender, WebSocketConnectionEventArgs e)
 {
     foreach (List <WebSocket> webSockets in this.subscriptions.Values)
     {
         foreach (WebSocket webSocket in webSockets)
         {
             if (e.WebSocket == webSocket)
             {
                 webSockets.Remove(e.WebSocket);
                 break;
             }
         }
     }
 }
Exemple #4
0
        public async void HandleWebSocketRequestAsync(HttpListenerContext context)
        {
            WebSocketConnectionEventArgs webSocketConnectionEventArgs = new WebSocketConnectionEventArgs(context, context.Request.RawUrl);

            try
            {
                this.Opened?.Invoke(this, webSocketConnectionEventArgs);

                if (webSocketConnectionEventArgs.IsHandled == false)
                {
                    Logging.Warning($"WebSocket is not supported: '{context.Request.RawUrl}'!");

                    HttpListenerRequest  request  = context.Request;
                    HttpListenerResponse response = context.Response;

                    response.StatusCode = (int)HttpStatusCode.NotFound;
                    byte[] buffer = Encoding.UTF8.GetBytes($"WebSocket is not supported: '{context.Request.RawUrl}'!");

                    response.ContentLength64 = buffer.Length;
                    Stream output = response.OutputStream;
                    output.Write(buffer, 0, buffer.Length);
                    output.Close();

                    return;
                }

                WebSocketContext webSocketContext = await context.AcceptWebSocketAsync(null);

                webSocketConnectionEventArgs.WebSocket = webSocketContext.WebSocket;
                if (this.webSockets.ContainsKey(webSocketConnectionEventArgs.Url) == false)
                {
                    this.webSockets.Add(webSocketConnectionEventArgs.Url, new List <WebSocket>());
                }
                this.webSockets[webSocketConnectionEventArgs.Url].Add(webSocketConnectionEventArgs.WebSocket);

                string message = string.Empty;

                byte[] receiveBuffer = new byte[RECEIVE_BUFFER_SIZE];
                int    messageLength = 0;
                while (webSocketConnectionEventArgs.WebSocket.State == WebSocketState.Open)
                {
                    WebSocketReceiveResult receiveResult = await webSocketConnectionEventArgs.WebSocket.ReceiveAsync(new ArraySegment <byte>(receiveBuffer), CancellationToken.None);

                    messageLength += receiveResult.Count;

                    if (receiveResult.MessageType == WebSocketMessageType.Close)
                    {
                        await webSocketConnectionEventArgs.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
                    }
                    else if (receiveResult.MessageType != WebSocketMessageType.Text)
                    {
                        await webSocketConnectionEventArgs.WebSocket.CloseAsync(WebSocketCloseStatus.InvalidMessageType, "Only accept text frame", CancellationToken.None);
                    }
                    else
                    {
                        message += Encoding.UTF8.GetString(receiveBuffer, 0, messageLength);

                        if (receiveResult.EndOfMessage)
                        {
                            this.Received?.Invoke(this, new WebSocketReceivedEventArgs(webSocketConnectionEventArgs.WebSocket, webSocketConnectionEventArgs.Url, message));
                            message       = string.Empty;
                            messageLength = 0;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Error("Error while handling Websocket request!", ex);
            }
            finally
            {
                if (webSocketConnectionEventArgs.WebSocket != null)
                {
                    this.Closed?.Invoke(this, webSocketConnectionEventArgs);
                    this.webSockets[webSocketConnectionEventArgs.Url].Remove(webSocketConnectionEventArgs.WebSocket);
                    webSocketConnectionEventArgs.WebSocket.Dispose();
                }
            }
        }