我很高兴能够使用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和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说:
发送最后一个块以指示流的结束后,您无需担心会话超时。该服务继续处理音频,直到它返回最终的转录结果。转录长音频流时,该服务可能需要30秒以上的时间来处理音频并生成响应。在完成处理收到的所有音频之前,服务不会开始计算会话超时。服务的处理时间不会导致会话超过30秒的会话超时。
以下代码似乎对我有用:
/**
* @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);
}
}