AsyncTcpConnection 发起websocket连接 发送音频流数据 报错

zhou604638018

问题描述

使用AsyncTcpConnection 作为websocket客户端,发送音频流数据。

程序代码

 // 创建异步TCP连接
        $connection = new AsyncTcpConnection($this->url);
//        $connection->maxSendBufferSize = 1048576 * 20;
        $connection->transport = 'ssl';
// 当连接建立时发送数据
        $connection->onWebSocketConnect = function (AsyncTcpConnection $connect, $response) {
            Log::info('腾讯实时asr Connected to Tencent AI Recognition' . $response);

        };

// 当接收到数据时
        $connection->onMessage = function (AsyncTcpConnection $connect, $message) {
            static $ok = true;
            Log::info($message);
            if ($ok) {
                $ok = false;

                $audioFilePath = "/home/www/webman/public/test.pcm"; // 请替换为您的音频文件路径
                $audioData = file_get_contents($audioFilePath);

                // 分片大小,根据腾讯云文档设置
                $chunkSize = 1280; // 举例,具体大小请参照腾讯云文档
                $totalSize = strlen($audioData);

                Log::info('发送音频数据:' . $totalSize);
                // 分片发送
                for ($i = 0; $i < $totalSize; $i += $chunkSize) {
                    $chunkData = substr($audioData, $i, $chunkSize);
//                Log::info('', ['len' => strlen($chunkData)]);
                    // 发送分片
                   $flag = $connect->send($chunkData, true);
                    Log::info('', ['flag' => $flag]);
                    usleep(40 * 1000);
                }

                // 发送结束标志
//            $connection->send('{"end": true}');

                Log::info('发送结束数据');
                $connect->send('{"type":"end"}');

            }
        };

报错信息

send buffer full and drop package
320 1 0
1个回答

walkor 打赏

发送缓冲区满了,参见文档
https://www.workerman.net/doc/workerman/worker/on-buffer-full.html

另外workerman文档开发者必读里有说,workerman里不能用sleep语句。要延迟发送用定时。

×
🔝