workerman4.x withFile发送文件流问题

zhqing

$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);
以上这样设置无法使用流来发送
因为Safari浏览器一定要使用流才能播放mp3 mp4等.
如何解决?

2356 3 0
3个回答

zhqing

我以前在3的板本里面找了代码出来做了一个流可以发送。但在4的板本中无法使用session
使用Response就可以使用session

  • 暂无评论
walkor

session的作用是?

  • 暂无评论
walkor

更新到主干版本 https://github.com/walkor/Workerman 4.0.3
4.0.3刚刚给response->withFile()增加了$offset_start, $length 参数。

$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);
};
  • zhqing 2020-03-21

    好的,谢谢,我试试

  • zhqing 2020-03-22

    Safari浏览器都测试了,完全没那问题,我要捐赠支持

  • walkor 2020-03-22

    好的,感谢支持。

年代过于久远,无法发表回答
🔝