private async Task ParseWebSocketAsync( byte[] b, HttpParserDelegate parserDelegate, HttpCombinedParser parserHandler, IEnumerable <string> subProtocols, IObserver <string> obs) { switch (_dataReceiveState) { case DataReceiveState.IsListeningForHandShake: _handshakeParser.Parse(b, parserDelegate, parserHandler); HandshakeController(parserDelegate, subProtocols); break; case DataReceiveState.IsListening: await _textDataParser.ParseAsync(_tcpStream, b[0], ExcludeZeroApplicationDataInPong); if (_textDataParser.IsCloseReceived) { StopReceivingData(); } if (_textDataParser.HasNewMessage) { obs.OnNext(_textDataParser.NewMessage); } break; } }
internal IObservable <string> CreateObservableListener( CancellationTokenSource innerCancellationTokenSource, ITcpSocketClient tcpSocketClient, IEnumerable <string> subProtocols = null) { var parserDelegate = new HttpParserDelegate(); var parserHandler = new HttpCombinedParser(parserDelegate); TextDataParser.Reinitialize(); _innerCancellationTokenSource = innerCancellationTokenSource; _tcpSocketClient = tcpSocketClient; DataReceiveMode = DataReceiveMode.IsListeningForHandShake; IsConnected = false; _isHandshaking = true; WatchHandshakeForTimeout(parserDelegate, _innerCancellationTokenSource); var observable = Observable.Create <string>( obs => { var disp = _tcpSocketClient.ReadStream.ReadOneByteAtTheTimeObservable(innerCancellationTokenSource, isConnectionOpen: IsConnected || _isHandshaking) .Subscribe( b => { if (TextDataParser.IsCloseRecieved) { return; } if (_hasHandshakeTimedout) { _subjectConnectionStatus.OnNext(ConnectionStatus.Aborted); throw new WebsocketClientLiteException("Connection request to server timed out"); } switch (DataReceiveMode) { case DataReceiveMode.IsListeningForHandShake: _handshakeParser.Parse(b, parserDelegate, parserHandler); HandshakeController(parserDelegate, subProtocols); break; case DataReceiveMode.IsListeningForTextData: TextDataParser.Parse(_tcpSocketClient, b[0], ExcludeZeroApplicationDataInPong); if (TextDataParser.IsCloseRecieved) { StopReceivingData(); } if (TextDataParser.HasNewMessage) { obs.OnNext(TextDataParser.NewMessage); } break; } }, ex => { StopReceivingData(); throw ex; }, () => { // ReSharper disable once ConvertClosureToMethodGroup StopReceivingData(); } ); HasReceivedCloseFromServer = false; return(disp); }); return(observable); }