我在websocket上获得响应时遇到问题

时间:2019-06-16 17:41:29

标签: c# websocket

我使用websocket API进行语音识别。 我将音频发送到服务器并收到响应。 使用此websocket API,可以在流式传输语音数据时获得语音识别结果。 我可以获得最终的识别结果,但是在途中无法获得识别结果。

以下显示了引用的代码。 https://gist.github.com/arjun-g/75961830d363cc9265e4ac2ca095168b

经过各种试验,我发现line:85是一个问题。 这部分似乎正在等待最终结果。

我可能根本没有收到结果。

请告诉我为什么我在途中无法获得结果。

private async Task Connect(ClientWebSocket clientWebSocket)
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            clientWebSocket.Options.SetRequestHeader("Authorization", $"Bearer {this.accessToken}");        
            Uri connection = new Uri($"wss://hogehoge");
            await clientWebSocket.ConnectAsync(connection, cts.Token);
        }

以上是connect方法的一部分。

这是我的完整代码。

private async Task Connect(ClientWebSocket clientWebSocket)
        {

            CancellationTokenSource cts = new CancellationTokenSource();
            clientWebSocket.Options.SetRequestHeader("Authorization", $"Bearer {this.accessToken}");
            Uri connection = new Uri($"wss://hogehoge");
            await clientWebSocket.ConnectAsync(connection, cts.Token);
        }

static async Task SendAudio(ClientWebSocket ws)
        {
            ArraySegment<byte> closingMessage = new ArraySegment<byte>(Encoding.UTF8.GetBytes(
            "{\"command\": \"recog-break\"}"
            ));
            using (FileStream fs = File.OpenRead("audio.raw"))
            {
                byte[] b = new byte[3200];
                while (fs.Read(b, 0, b.Length) > 0)
                {
                    await ws.SendAsync(new ArraySegment<byte>(b), WebSocketMessageType.Binary, true, CancellationToken.None);
                }
                await ws.SendAsync(closingMessage, WebSocketMessageType.Text, true, CancellationToken.None);
            }
        }

        // prints results until the connection closes or a delimeterMessage is recieved

private async Task HandleResults(ClientWebSocket ws)
        {
            var buffer = new byte[60000];
            while (true)
            {
                var segment = new ArraySegment<byte>(buffer);

                var result = await ws.ReceiveAsync(segment, CancellationToken.None);

                if (result.MessageType == WebSocketMessageType.Close)
                {
                    return;
                }

                int count = result.Count;
                while (!result.EndOfMessage)
                {
                    if (count >= buffer.Length)
                    {
                        await ws.CloseOutputAsync(WebSocketCloseStatus.InvalidPayloadData, "That's too long", 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);

                this.textBox2.AppendText(message);

            }
        }

private async void button1_Click_1(object sender, EventArgs e)
        {
            ClientWebSocket cws = new ClientWebSocket();
            await this.Connect(cws);
            await Task.WhenAll(SendAudio(cws), HandleResults(cws));
        }

0 个答案:

没有答案