Beispiel #1
0
        public async Task <Task> AcceptConnectionAsync(WebSocket websocket)
        {
            var connection = new WebSocketServerTransmissionConnection(websocket, CancellationToken);
            await _buffer.Out.WriteAsync(connection, CancellationToken).ConfigureAwait(false);

            Log.Trace("Websocket connection accepted");
            return(connection.Completion);
        }
Beispiel #2
0
        private void OnSocketConnection(IWebSocketConnection websocket)
        {
            Log.Debug("Handling websocket connection {0}: path={1}", websocket.ConnectionInfo.Id, websocket.ConnectionInfo.Path);
            var urlPath = websocket.ConnectionInfo.Path.TrimEnd('/');

            if (string.IsNullOrEmpty(urlPath))
            {
                var connection = new WebSocketServerTransmissionConnection(websocket);
                TaskRunner.RunInBackground(AcceptWebSocketConnectionAsync, connection).IgnoreAwait(Log);
            }
            else
            {
                TaskRunner.RunInBackground(() => ReadFileAsync(websocket, urlPath)).IgnoreAwait(Log);
            }
        }
        private void OnSocketConnection(IWebSocketConnection websocket)
        {
            Log.Debug("Handling websocket connection {0}: path={1}", websocket.ConnectionInfo.Id, websocket.ConnectionInfo.Path);
            var urlPath = websocket.ConnectionInfo.Path.TrimEnd('/');

            if (string.IsNullOrEmpty(urlPath))
            {
                var connection = new WebSocketServerTransmissionConnection(websocket);
                TaskRunner.RunInBackground(AcceptWebSocketConnectionAsync, connection).IgnoreAwait(Log);
            }
            else
            {
                var connectedCompletion = new Promise();
                void OnOpened() => connectedCompletion.TryComplete();
                void OnClosed() => connectedCompletion.TryFail(new Exception("Websocket unexpectedly closed"));
                void OnError(Exception ex) => connectedCompletion.TryFail(new Exception("Websocket exception occurred", ex));

                websocket.OnOpen  = OnOpened;
                websocket.OnClose = OnClosed;
                websocket.OnError = OnError;
                TaskRunner.RunInBackground(async() =>
                {
                    try
                    {
                        await connectedCompletion.Task;
                    }
                    finally
                    {
                        // ReSharper disable DelegateSubtraction
                        websocket.OnOpen  -= OnOpened;
                        websocket.OnClose -= OnClosed;
                        websocket.OnError -= OnError;
                        // ReSharper restore DelegateSubtraction
                    }
                    await ReadFileAsync(websocket, urlPath);
                }).IgnoreAwait(Log);
            }
        }