/// <summary> /// Listens for messages received from the service and handles them appropriately. /// </summary> /// <param name="webSocket">The connected WebSocket on which to listen for the message.</param> /// <param name="requestId">The request ID associated with the conversation.</param> /// <param name="token">A task cancellation token.</param> /// <returns>A Task representing the asynchronous operation.</returns> private async Task ReceiveAsync(WebSocket webSocket, string requestId, CancellationToken token) { try { while (webSocket.State == WebSocketState.Open && !token.IsCancellationRequested) { var speechServiceMessage = await this.ReceiveMessageAsync(webSocket, requestId, token); if (speechServiceMessage is SpeechPhraseMessage) { RecognitionResult phraseResponse = new RecognitionResult((SpeechPhraseMessage)speechServiceMessage); this.OnResponseReceived?.Invoke(this, new SpeechResponseEventArgs(phraseResponse)); } else if (speechServiceMessage is SpeechHypothesisMessage) { string partialResponse = ((SpeechHypothesisMessage)speechServiceMessage).Text; this.OnPartialResponseReceived?.Invoke(this, new PartialSpeechResponseEventArgs(partialResponse)); } else if (speechServiceMessage is SpeechFragmentMessage) { string partialResponse = ((SpeechFragmentMessage)speechServiceMessage).Text; this.OnPartialResponseReceived?.Invoke(this, new PartialSpeechResponseEventArgs(partialResponse)); } else if (speechServiceMessage is TurnEndMessage || speechServiceMessage == null) { await this.CloseConnectionAsync(); // End of turn - stop listening break; } } } catch (WebSocketException e) { this.RaiseTranslateError(e); } catch (OperationCanceledException) { } finally { webSocket.Dispose(); } }
/// <summary> /// Initializes a new instance of the <see cref="SpeechResponseEventArgs"/> class. /// </summary> /// <param name="result">The speech recognition result.</param> internal SpeechResponseEventArgs(RecognitionResult result) { this.result = result; }