Example #1
0
        public void Start(Action <IWebSocketConnection> config)
        {
            var ipLocal = new IPEndPoint(_locationIP, Port);

            ListenerSocket.Bind(ipLocal);
            ListenerSocket.Listen(100);
            Port = ((IPEndPoint)ListenerSocket.LocalEndPoint).Port;
            NTWebSocketLog.Info(string.Format("Server started at {0} (actual port {1})", Location, Port));
            if (_scheme == "wss")
            {
                if (Certificate == null)
                {
                    NTWebSocketLog.Error("Scheme cannot be 'wss' without a Certificate");
                    return;
                }

                if (EnabledSslProtocols == SslProtocols.None)
                {
                    EnabledSslProtocols = SslProtocols.Tls;
                    NTWebSocketLog.Debug("Using default TLS 1.0 security protocol.");
                }
            }
            ListenForClients();
            _config = config;
        }
Example #2
0
 private void ListenForClients()
 {
     ListenerSocket.Accept(OnClientConnect, e => {
         NTWebSocketLog.Error("Listener socket is closed", e);
         if (RestartAfterListenError)
         {
             NTWebSocketLog.Info("Listener socket restarting");
             try {
                 ListenerSocket.Dispose();
                 var socket     = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);
                 ListenerSocket = new SocketWrapper(socket);
                 Start(_config);
                 NTWebSocketLog.Info("Listener socket restarted");
             }
             catch (Exception ex) {
                 NTWebSocketLog.Error("Listener could not be restarted", ex);
             }
         }
     });
 }
Example #3
0
 private Task SendBytes(byte[] bytes, Action callback = null)
 {
     return(Socket.Send(bytes, () => {
         NTWebSocketLog.Debug("Sent " + bytes.Length + " bytes");
         if (callback != null)
         {
             callback();
         }
     },
                        e => {
         if (e is IOException)
         {
             NTWebSocketLog.Debug("Failed to send. Disconnecting.", e);
         }
         else
         {
             NTWebSocketLog.Info("Failed to send. Disconnecting.", e);
         }
         CloseSocket();
     }));
 }