private void OnConnectionClosed(object sender, WebSocketConnectionEventArgs args)
        {
            var app = _applications.GetApplicationByHost(args.Connection.Host);

            if (app != null)
            {
                app.NotifyConnectionClosed(args.Connection);
            }
        }
        public override void Start(string url, string sslCertificateSerialNumber)
        {
            var uri = new Uri(url);
            var listenUrl = string.Format("{0}://0.0.0.0:{1}", uri.Scheme, uri.Port);
            _webSocketServer = new WebSocketServer(listenUrl);

            if (sslCertificateSerialNumber != null)
            {
                var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                var certificate = store.Certificates
                    .Find(X509FindType.FindBySerialNumber, sslCertificateSerialNumber, false)
                    .OfType<X509Certificate2>()
                    .FirstOrDefault();

                _webSocketServer.Certificate = certificate;
            }

            _logger.Info("Starting WebSocket server");
            _webSocketServer.Start(c =>
            {
                c.OnOpen = () =>
                    {
                        _logger.Debug("Opened connection: " + c.ConnectionInfo.Id);
                        RegisterConnection(new FleckWebSocketConnection(c));
                    };
                c.OnClose = () =>
                    {
                        _logger.Debug("Closed connection: " + c.ConnectionInfo.Id);
                        UnregisterConnection(c.ConnectionInfo.Id);
                    };
                
                c.OnMessage = msg =>
                    {
                        _logger.Debug("Received message for connection: " + c.ConnectionInfo.Id);

                        var fc = WaitConnection(c.ConnectionInfo.Id);
                        if (fc == null)
                        {
                            _logger.ErrorFormat("Connection {0} is not registered", c.ConnectionInfo.Id);
                            return;
                        }

                        var e = new WebSocketMessageEventArgs(fc, msg);
                        OnMessageReceived(e);
                    };

                c.OnPing = data =>
                    {
                        _logger.Debug("Received ping for connection: " + c.ConnectionInfo.Id);

                        var fc = WaitConnection(c.ConnectionInfo.Id);
                        if (fc == null)
                        {
                            _logger.ErrorFormat("Connection {0} is not registered", c.ConnectionInfo.Id);
                            return;
                        }

                        var e = new WebSocketConnectionEventArgs(fc);
                        OnPingReceived(e);

                        c.SendPong(data);
                    };
            });
        }
Esempio n. 3
0
        public override void Start(string url, string sslCertificateSerialNumber)
        {
            var uri       = new Uri(url);
            var listenUrl = string.Format("{0}://0.0.0.0:{1}", uri.Scheme, uri.Port);

            _webSocketServer = new WebSocketServer(listenUrl);

            if (sslCertificateSerialNumber != null)
            {
                var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                var certificate = store.Certificates
                                  .Find(X509FindType.FindBySerialNumber, sslCertificateSerialNumber, false)
                                  .OfType <X509Certificate2>()
                                  .FirstOrDefault();

                _webSocketServer.Certificate = certificate;
            }

            _logger.Info("Starting WebSocket server");
            _webSocketServer.Start(c =>
            {
                c.OnOpen = () =>
                {
                    _logger.Debug("Opened connection: " + c.ConnectionInfo.Id);
                    RegisterConnection(new FleckWebSocketConnection(c));
                };
                c.OnClose = () =>
                {
                    _logger.Debug("Closed connection: " + c.ConnectionInfo.Id);
                    UnregisterConnection(c.ConnectionInfo.Id);
                };

                c.OnMessage = msg =>
                {
                    _logger.Debug("Received message for connection: " + c.ConnectionInfo.Id);

                    var fc = WaitConnection(c.ConnectionInfo.Id);
                    if (fc == null)
                    {
                        _logger.ErrorFormat("Connection {0} is not registered", c.ConnectionInfo.Id);
                        return;
                    }

                    var e = new WebSocketMessageEventArgs(fc, msg);
                    OnMessageReceived(e);
                };

                c.OnPing = data =>
                {
                    _logger.Debug("Received ping for connection: " + c.ConnectionInfo.Id);

                    var fc = WaitConnection(c.ConnectionInfo.Id);
                    if (fc == null)
                    {
                        _logger.ErrorFormat("Connection {0} is not registered", c.ConnectionInfo.Id);
                        return;
                    }

                    var e = new WebSocketConnectionEventArgs(fc);
                    OnPingReceived(e);

                    c.SendPong(data);
                };
            });
        }