LiveCaptionTranslator/LiveCaptionTranslator/Models/IBMWatson/IBMWatsonSpeechToTextWebsocketService.cs
/// <summary>
/// recieve websocket message until by websocket is closed.
/// </summary>
/// <returns></returns>
recieve websocket message until by websocket is closed.
recieve websocket message until by websocket is closed .
public async Task HandleCallback()
        {
            var buffer = new byte[1048576];
            while (true)
            {
                var segment = new ArraySegment<byte>(buffer);
                var result = await ws.ReceiveAsync(segment, CancellationToken.None);
                if (result.MessageType == WebSocketMessageType.Close) return;
                var count = result.Count;
                while (!result.EndOfMessage)
                {
                    if (count >= buffer.Length)
                    {
                        await ws.CloseAsync(WebSocketCloseStatus.InvalidPayloadData, "count >= buffer.Length!!!!!", CancellationToken.None);
                        return;
                    }
                    segment = new ArraySegment<byte>(buffer, count, buffer.Length - count);
                    result = await ws.ReceiveAsync(segment, CancellationToken.None);
                    count += result.Count;
                }
                var message = Encoding.UTF8.GetString(buffer, 0, count);
                if (IsDelimeter(message)) return;
                var jsonObj = JsonConvert.DeserializeObject<StreamingRecognizeResponse>(message);
                Transcipt = jsonObj.results.FirstOrDefault().alternatives.FirstOrDefault().transcript;
                ResultIndex = jsonObj.result_index;
            }
        }
public async Task HandleCallback ( ) { var buffer = new byte [ 1048576 ] ; while ( true ) { var segment = new ArraySegment < byte > ( buffer ) ; var result = await ws . ReceiveAsync ( segment , CancellationToken . None ) ; if ( result . MessageType == WebSocketMessageType . Close ) return ; var count = result . Count ; while ( ! result . EndOfMessage ) { if ( count >= buffer . Length ) { await ws . CloseAsync ( WebSocketCloseStatus . InvalidPayloadData , " " , CancellationToken . None ) ; return ; } segment = new ArraySegment < byte > ( buffer , count , buffer . Length - count ) ; result = await ws . ReceiveAsync ( segment , CancellationToken . None ) ; count += result . Count ; } var message = Encoding . UTF8 . GetString ( buffer , 0 , count ) ; if ( IsDelimeter ( message ) ) return ; var jsonObj = JsonConvert . DeserializeObject < StreamingRecognizeResponse > ( message ) ; Transcipt = jsonObj . results . FirstOrDefault ( ) . alternatives . FirstOrDefault ( ) . transcript ; ResultIndex = jsonObj . result_index ; } }
recieve websocket message until by websocket is closed.
recieve websocket message until by websocket is closed .
None