Esempio n. 1
0
 private void OnWebSocketConnecting(WebSocketConnectingEventArgs args)
 {
     if (WebSocketConnecting != null)
     {
         WebSocketConnecting(this, args);
     }
 }
Esempio n. 2
0
        private async Task ProcessWebSocketRequest(HttpListenerContext ctx)
        {
            try
            {
                var endpoint = ctx.Request.RemoteEndPoint.ToString();
                var url      = ctx.Request.RawUrl;

                var queryString = ctx.Request.QueryString;

                var connectingArgs = new WebSocketConnectingEventArgs
                {
                    Url         = url,
                    QueryString = queryString,
                    Endpoint    = endpoint
                };

                if (WebSocketConnecting != null)
                {
                    WebSocketConnecting(connectingArgs);
                }

                if (connectingArgs.AllowConnection)
                {
                    _logger.LogDebug("Web socket connection allowed");

                    var webSocketContext = await ctx.AcceptWebSocketAsync(null).ConfigureAwait(false);

                    if (WebSocketConnected != null)
                    {
                        var socket = new SharpWebSocket(webSocketContext.WebSocket, _logger);

                        WebSocketConnected(new WebSocketConnectEventArgs
                        {
                            Url         = url,
                            QueryString = queryString,
                            WebSocket   = socket,
                            Endpoint    = endpoint
                        });

                        await ReceiveWebSocket(ctx, socket).ConfigureAwait(false);
                    }
                }
                else
                {
                    _logger.LogWarning("Web socket connection not allowed");
                    ctx.Response.StatusCode = 401;
                    ctx.Response.Close();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "AcceptWebSocketAsync error");
                ctx.Response.StatusCode = 500;
                ctx.Response.Close();
            }
        }
Esempio n. 3
0
        private void OnWebSocketConnecting(WebSocketConnectingEventArgs args)
        {
            if (_disposed)
            {
                return;
            }

            if (WebSocketConnecting != null)
            {
                WebSocketConnecting(this, args);
            }
        }
Esempio n. 4
0
        private void ProcessWebSocketRequest(HttpListenerContext ctx)
        {
            try
            {
                var endpoint    = ctx.Request.RemoteEndPoint.ToString();
                var url         = ctx.Request.RawUrl;
                var queryString = new NameValueCollection(ctx.Request.QueryString ?? new NameValueCollection());

                var connectingArgs = new WebSocketConnectingEventArgs
                {
                    Url         = url,
                    QueryString = queryString,
                    Endpoint    = endpoint
                };

                if (WebSocketConnecting != null)
                {
                    WebSocketConnecting(connectingArgs);
                }

                if (connectingArgs.AllowConnection)
                {
                    _logger.Debug("Web socket connection allowed");

                    var webSocketContext = ctx.AcceptWebSocket(null);

                    if (WebSocketConnected != null)
                    {
                        WebSocketConnected(new WebSocketConnectEventArgs
                        {
                            Url         = url,
                            QueryString = queryString,
                            WebSocket   = new SharpWebSocket(webSocketContext.WebSocket, _logger),
                            Endpoint    = endpoint
                        });
                    }
                }
                else
                {
                    _logger.Warn("Web socket connection not allowed");
                    ctx.Response.StatusCode = 401;
                    ctx.Response.Close();
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException("AcceptWebSocketAsync error", ex);
                ctx.Response.StatusCode = 500;
                ctx.Response.Close();
            }
        }
Esempio n. 5
0
        async void _httpServer_WebSocketConnecting(object sender, WebSocketConnectingEventArgs e)
        {
            var token = e.QueryString["api_key"];

            if (!string.IsNullOrWhiteSpace(token))
            {
                var session = await GetSession(e.QueryString, e.Endpoint).ConfigureAwait(false);

                if (session == null)
                {
                    //e.AllowConnection = false;
                }
            }
        }
Esempio n. 6
0
        async void _httpServer_WebSocketConnecting(object sender, WebSocketConnectingEventArgs e)
        {
            //var token = e.QueryString["api_key"];
            //if (!string.IsNullOrWhiteSpace(token))
            //{
            //    try
            //    {
            //        var session = await GetSession(e.QueryString, e.Endpoint).ConfigureAwait(false);

            //        if (session == null)
            //        {
            //            e.AllowConnection = false;
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        _logger.ErrorException("Error getting session info", ex);
            //    }
            //}
        }