<?php require_once '../../src/libsse.php'; class TimeEvent extends SSEEvent { public function update() { return date('l, F jS, Y, h:i:s A'); } } $sse = new SSE(); $sse->exec_limit = 10; $sse->addEventListener('time', new TimeEvent()); $sse->start();
public function check() { $this->data = json_decode($GLOBALS['data']->get($_GET['id'])); if ($this->data->updated !== $this->cache) { $this->cache = $this->data->updated; return true; } return false; } } $sse->exec_limit = 0; $sse->addEventListener('gameUpdate', new CurrentGame()); $sse->start(); } else { $GLOBALS['data'] = new SSEData('file', array('path' => './data', 'gc_lifetime' => 16000)); $sse = new SSE(); class CurrentGames extends SSEEvent { private $cache = 0; private $data; public function update() { $status = $GLOBALS['data']->get('status'); if (strlen($status) < 1) { return json_encode(array('games' => array())); } else { return $status; } } public function check() {
function get_server_load() { if (stristr(PHP_OS, 'win')) { $wmi = new COM("Winmgmts://"); $server = $wmi->execquery("SELECT LoadPercentage FROM Win32_Processor"); $cpu_num = 0; $load_total = 0; foreach ($server as $cpu) { $cpu_num++; $load_total += $cpu->loadpercentage; } $load = round($load_total / $cpu_num); } else { $sys_load = sys_getloadavg(); $load = $sys_load[0]; } return (int) $load; } class SysEvent extends SSETimedEvent { //Beware: use SSETimedEvent for sending data at a regular interval public $period = 5; //the interval in seconds public function update() { return json_encode(array('load' => get_server_load(), 'time' => time())); } } $sse = new SSE(); $sse->addEventListener('data', new SysEvent()); $sse->start();
<?php require_once '../../src/libsse.php'; $GLOBALS['data'] = new SSEData('file', array('path' => './data')); $sse = new SSE(); class LatestUser extends SSEEvent { private $cache = 0; private $data; public function update() { return $this->data->msg; } public function check() { $this->data = json_decode($GLOBALS['data']->get('user')); if ($this->data->time !== $this->cache) { $this->cache = $this->data->time; return true; } return false; } } class LatestMessage extends SSEEvent { private $cache = 0; private $data; public function update() { return json_encode($this->data); }