juggl/public/api/events.php
2021-02-18 14:03:59 +01:00

44 lines
No EOL
1.6 KiB
PHP

<?php
require_once(__DIR__.'/vendor/autoload.php');
require_once(__DIR__."/services/authenticator.inc.php");
require_once(__DIR__."/services/responses.inc.php");
use Sse\Event;
use Sse\SSE;
//create the event handler
class YourEventHandler implements Event {
public function update(){
//Here's the place to send data
return 'Hello, world!';
}
public function check(){
//Here's the place to check when the data needs update
return true;
}
}
$SSE_KEY_KEY = "sse_key";
$auth = new Authenticator();
if (!isset($_GET["user_id"]) || !isset($_GET[$SSE_KEY_KEY]) || !$auth->isSseAuthenticated($_GET["user_id"], $_GET[$SSE_KEY_KEY])) {
respondStatus(403);
}
$sse = new SSE(); //create a libSSE instance
$sse->exec_limit = 600; //the execution time of the loop in seconds. Default: 600. Set to 0 to allow the script to run as long as possible.
$sse->sleep_time = 5; //The time to sleep after the data has been sent in seconds. Default: 0.5.
$sse->client_reconnect = 10; //the time for the client to reconnect after the connection has lost in seconds. Default: 1.
$sse->use_chunked_encoding = false; //Use chunked encoding. Some server may get problems with this and it defaults to false
$sse->keep_alive_time = 600; //The interval of sending a signal to keep the connection alive. Default: 300 seconds.
$sse->allow_cors = true; //Allow cross-domain access? Default: false. If you want others to access this must set to true.
$sse->addEventListener('change', new YourEventHandler());//register your event handler
$sse->start();//start the event loop
?>