/// <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) { PartialRecognitionResult partialResponse = new PartialRecognitionResult((SpeechHypothesisMessage)speechServiceMessage); this.OnPartialResponseReceived?.Invoke(this, new PartialSpeechResponseEventArgs(partialResponse)); } else if (speechServiceMessage is SpeechFragmentMessage) { PartialRecognitionResult partialResponse = new PartialRecognitionResult((SpeechFragmentMessage)speechServiceMessage); this.OnPartialResponseReceived?.Invoke(this, new PartialSpeechResponseEventArgs(partialResponse)); } else if (speechServiceMessage is TurnEndMessage || speechServiceMessage == null) { // End of turn - stop listening break; } } } catch (WebSocketException e) { this.RaiseTranslateError(e); } catch (OperationCanceledException) { } finally { await this.CloseConnectionAsync(webSocket); } }
/// <summary> /// Initializes a new instance of the <see cref="PartialSpeechResponseEventArgs"/> class. /// </summary> /// <param name="partialResult">The partial speech recognition result text.</param> internal PartialSpeechResponseEventArgs(PartialRecognitionResult partialResult) { this.PartialResult = partialResult; }