ratchetphp/Pawl 连接关闭 (1009 - ) FRAME::CLOSE_TOO_BIG = 1009

ratchetphp/Pawl Connection closed (1009 - ) FRAME::CLOSE_TOO_BIG = 1009

我很高兴能使用 Pawl,我让它可以处理小文件(例如 350 KB)。

但是,当我如下所示通过 $fullFileName 发送更大的文件(比如 30 MB)时,我收到此错误:Connection closed (1009 - ).

\Ratchet\Client\connect($url)->then(function(\Ratchet\Client\WebSocket $conn) use($contentType, $fullFileName) {
    $conn->on('message', function($msg) use ($conn) {
        Log::debug("Received: {$msg}");
    });
    $conn->on('close', function($code = null, $reason = null) {
        Log::debug("Connection closed ({$code} - {$reason})");
    });
    $conn->send(json_encode([
        'content-type' => $contentType,
        'timestamps' => true,
        'speaker_labels' => true,
        'smart_formatting' => true,
        'inactivity_timeout' => 60 * 5,
        'x-watson-learning-opt-out' => true,
        'action' => 'start'
    ]));

    $frame = new \Ratchet\RFC6455\Messaging\Frame(file_get_contents($fullFileName), true, \Ratchet\RFC6455\Messaging\Frame::OP_BINARY);
    $binaryMsg = new \Ratchet\RFC6455\Messaging\Message();
    $binaryMsg->addFrame($frame);
    $conn->send($binaryMsg);
    $conn->send(json_encode([
        'action' => 'stop'
    ]));
}, function ($e) {
    echo "Could not connect: {$e->getMessage()}\n";
});

当我搜索 Frame::CLOSE_TOO_BIG 的用法时,我发现它只被 \Ratchet\RFC6455\Messaging\CloseFrameChecker 使用过。

但我一直无法弄清楚 \Ratchet\RFC6455\Messaging\CloseFrameChecker 是如何工作的,文件大小限制是多少,以及如何发送大文件。

我尝试先使用 str_split 将我的文件拆分成多个块,然后添加单独的帧,但后来我每次都遇到会话超时,即使是小文件也是如此。

发送较大文件的适当方法是什么,避免 Frame::CLOSE_TOO_BIG 1009 错误和会话超时?

https://cloud.ibm.com/apidocs/speech-to-text and https://cloud.ibm.com/docs/services/speech-to-text?topic=speech-to-text-websockets#WSreturn 说 1009 = 由于帧大小超过 4 MB 限制,连接关闭。

所以我尝试将音频文件拆分成单独的帧。我的第一次尝试总是导致 "Session timed out." 错误。

https://cloud.ibm.com/docs/services/speech-to-text?topic=speech-to-text-input#timeouts 说:

You do not need to worry about the session timeout after you send the last chunk to indicate the end of the stream. The service continues to process the audio until it returns the final transcription results. When you transcribe a long audio stream, the service can take more than 30 seconds to process the audio and generate a response. The service does not begin to calculate the session timeout until it finishes processing all audio that it has received. The service's processing time cannot cause the session to exceed the 30-second session timeout.

下面是似乎对我有用的代码:

/**
 * @param \Ratchet\Client\WebSocket $conn
 * @param string $fileContents
 */
public function sendBinaryMessage($conn, $fileContents) {
    $chunks = str_split($fileContents, 100);
    Log::debug('Split audio into this many frames: ' . count($chunks));
    $final = true;
    foreach ($chunks as $key => $chunk) {
        Log::debug('send chunk key ' . $key);
        $frame = new \Ratchet\RFC6455\Messaging\Frame($chunk, $final, \Ratchet\RFC6455\Messaging\Frame::OP_BINARY);
        $conn->send($frame);
    }
}