Beispiel #1
0
        private Task Send <T>(T message, Func <T, byte[]> createFrame)
        {
            if (Handler == null)
            {
                throw new InvalidOperationException("Cannot send before handshake");
            }

            if (!IsAvailable)
            {
                const string errorMessage = "Data sent while closing or after close. Ignoring.";
                NTWebSocketLog.Warn(errorMessage);

                var taskForException = new TaskCompletionSource <object>();
                taskForException.SetException(new ConnectionNotAvailableException(errorMessage));
                return(taskForException.Task);
            }

            var bytes = createFrame(message);

            return(SendBytes(bytes));
        }
Beispiel #2
0
        private void OnClientConnect(ISocket clientSocket)
        {
            if (clientSocket == null)
            {
                return;                       // socket closed
            }
            NTWebSocketLog.Debug(String.Format("Client connected from {0}:{1}", clientSocket.RemoteIpAddress, clientSocket.RemotePort.ToString()));
            ListenForClients();

            WebSocketConnection connection = null;

            connection = new WebSocketConnection(
                clientSocket,
                initialize: _config,
                parseRequest: bytes => RequestParser.Parse(bytes, _scheme),
                handlerFactory: r => HandlerFactory.BuildHandler(
                    request: r,
                    onMessage: s => connection.OnMessage(s),
                    onClose: connection.Close,
                    onBinary: b => connection.OnBinary(b),
                    onPing: b => connection.OnPing(b),
                    onPong: b => connection.OnPong(b)),
                negotiateSubProtocol: s => SubProtocolNegotiator.Negotiate(SupportedSubProtocols, s));

            if (IsSecure)
            {
                NTWebSocketLog.Debug("Authenticating Secure Connection");
                clientSocket
                .Authenticate(Certificate,
                              EnabledSslProtocols,
                              connection.StartReceiving,
                              e => NTWebSocketLog.Warn("Failed to Authenticate", e));
            }
            else
            {
                connection.StartReceiving();
            }
        }