覺得pipe也許可用,但是pipe只對TCPConnection有效,無法轉(zhuǎn)發(fā)stream
于是嘗試使用定時器
stream_set_timeout($stream,0,10);
$timer = Timer::add(0.5,function ($timer) use ($stream,$connection){
if(feof($stream)) fclose($stream) and Timer::del($timer);
else $connection->send(fread($stream,1*1024*1024),true);
},[$timer]);
然后意識到,send執(zhí)行后HTTP請求結(jié)束了,于是瀏覽器拿到了皮毛就返回了
找不到合適的方法,請教HTTP數(shù)據(jù)流可能嗎,該如何實現(xiàn)?
(我知道應(yīng)該降級到HTTP/1.0)
最后嘗試使用Chunk解決了流媒體的問題
$stream = popen("ffmpeg -i '$file' -f '$codec' pipe:",'rb');
stream_set_blocking($stream, false);
$connection->send(new Response(200, [
'Transfer-Encoding' => 'chunked',
'Content-Type' => $ctype
], ''));
$fd = Worker::$globalEvent->onReadable($stream, function () use ($connection,$stream,$fd){
// $connection->send(fgets($stream),true);
$connection->send(new Chunk(fgets($stream)));
if(feof($stream)) pclose($stream) and Worker::$globalEvent->offReadable($fd);
});
雖然不是使用文檔上的接口,可用就行了