$response = (new Response())->withFile($file);
$response->withHeaders([
'Connection' => 'keep-alive',
'Content-Type' => 'audio/mpeg',
'Accept-Ranges' => '0-8888322',
'Last-Modified' => 'Thu, 19 Mar 2020 16:48:04 Asia/Shanghai',
'Content-Length' => '8888322',
'Content-Range' => 'bytes 0-8888321/8888322'
]);
$connection->send($response);
以上這樣設(shè)置無法使用流來發(fā)送
因為Safari瀏覽器一定要使用流才能播放mp3 mp4等.
如何解決?
更新到主干版本 https://github.com/walkor/Workerman 4.0.3
。
4.0.3
剛剛給response->withFile()
增加了$offset_start
, $length
參數(shù)。
$worker->onMessage = function($connection, $request)
{
if (preg_match('/bytes=(\d*)-(\d*)/i', $request->header('range'), $match)) {
$offset_start = (int)$match[1];
$offset_end = $match[2];
$length = $offset_end === '' ? 0 : $offset_end - $offset_start + 1;
$response = (new Response())->withFile($file, $offset_start, $length);
} else {
$response = (new Response())->withFile($file);
}
$connection->send($response);
};