Home > Backend Development > PHP Tutorial > javascript - h5 has Server-sent Events, how to write it on the php side?

javascript - h5 has Server-sent Events, how to write it on the php side?

WBOY
Release: 2016-08-10 09:07:25
Original
1128 people have browsed it

<code>
public static function pushEvent(){
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache'); // 建议不要缓存SSE数据
    header('Connection: keep-alive');
    //header('X-Accel-Buffering: no');//有可能用nginx的需要设置这个才能把缓冲数据刷出去

    $serverTime = time();
    $hasData = hFile::cache('pushEvent');//读缓存
    if($hasData){
        hFile::cache('pushEvent',0);//存缓存
        self::sendMsg($serverTime, 'server time: ' . date("h:i:s"),'myevent',100000);
    }else
        self::sendMsg('','','',100);//多少毫秒内无数据,再重连
}


/**
 * @param string $id Timestamp/id of this connection.
 * @param string $msg Line of text that should be transmitted.
 * @param $event string
 */
private static function sendMsg($id='', $msg='',$event='',$retry=''){
    if(!($id||$event||$msg||$retry))
        return;

    if($retry) echo "retry: $retry" .PHP_EOL;
    if($id) echo "id: $id" . PHP_EOL;
    if($event) echo "event: {$event}" . PHP_EOL;
    if($msg) echo "data: $msg" . PHP_EOL;
    echo PHP_EOL;//表示本条数据结束
    ob_flush();
    flush();
}
</code>
Copy after login
Copy after login

I wrote it like this (part of the code was copied). The problem is that every time the front end establishes a connection, PHP can only return data once. If you want the next data, rely on this lineself::sendMsg('','','',100);//How many milliseconds There is no data in it, then reconnect. This line will tell the front end how many milliseconds it will take before re-establishing the connection. This seems to be a waste of resources, and each time you want to output data, you have to read the cache to see if there is a message sent to the user. Way, is there any other good way?

Reply content:

<code>
public static function pushEvent(){
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache'); // 建议不要缓存SSE数据
    header('Connection: keep-alive');
    //header('X-Accel-Buffering: no');//有可能用nginx的需要设置这个才能把缓冲数据刷出去

    $serverTime = time();
    $hasData = hFile::cache('pushEvent');//读缓存
    if($hasData){
        hFile::cache('pushEvent',0);//存缓存
        self::sendMsg($serverTime, 'server time: ' . date("h:i:s"),'myevent',100000);
    }else
        self::sendMsg('','','',100);//多少毫秒内无数据,再重连
}


/**
 * @param string $id Timestamp/id of this connection.
 * @param string $msg Line of text that should be transmitted.
 * @param $event string
 */
private static function sendMsg($id='', $msg='',$event='',$retry=''){
    if(!($id||$event||$msg||$retry))
        return;

    if($retry) echo "retry: $retry" .PHP_EOL;
    if($id) echo "id: $id" . PHP_EOL;
    if($event) echo "event: {$event}" . PHP_EOL;
    if($msg) echo "data: $msg" . PHP_EOL;
    echo PHP_EOL;//表示本条数据结束
    ob_flush();
    flush();
}
</code>
Copy after login
Copy after login

I wrote it like this (part of the code was copied). The problem is that every time the front end establishes a connection, PHP can only return data once. If you want the next data, rely on this lineself::sendMsg('','','',100);//How many milliseconds There is no data in it, then reconnect. This line will tell the front end how many milliseconds it will take before re-establishing the connection. This seems to be a waste of resources, and each time you want to output data, you have to read the cache to see if there is a message sent to the user. Way, is there any other good way?

Please refer to this article of mine: https://segmentfault.com/a/11...

The most important thing is the following paragraph:

<code>ob_flush();
flush();
//等待3秒钟,开始下一次查询
sleep(3);
</code>
Copy after login

After flushing, do not disconnect, but sleep for 3 seconds and then enter the loop again, so that the connection can be maintained and will not be disconnected and reconnected.

Why not do something like this via websocket?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template