1. Go to this page and download the library: Download htrnf/sse-server library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
htrnf / sse-server example snippets
SEServer\SSEServer;
use SSEServer\SSEControllerInterface;
class ExampleController implements SSEControllerInterface{
private $events;
function __construct(){
//We will store our Event objects into "Events"
$this->events = new Events;
}
/**
* Each cycle while running, this function will be called.
* This is where you should implement main logic for detecting new events
* and populate $this->events with them using $this->events->push().
*/
public function cycle(Int $currentCycle): Void{
//We will randomly send a message
$random = rand(0, 10);
switch($random){
case 0:
//Push the new event into the Events object
$this->events->push(new Event("message", Array("text" => "Hello")));
break;
case 1:
$this->events->push(new Event("message", Array("text" => "Howdy?")));
break;
case 2:
$this->events->push(new Event("message", Array("text" => "Do you like banana pancakes?")));
break;
case 3:
$this->events->merge(
new Events(
Array(
new Event("message", Array("text" => "Parrots are the coolest animals!")),
new Event("message", Array("text" => "I want to get one as a pet, do you know where i could get one?"))
)
)
);
break;
case 4:
$this->events->push(new Event("message", Array("text" => "Please go to the grocery store and get some bread.")));
break;
case 5:
$this->events->push(new Event("alert", Array("text" => "You have a scheduled meeting in 5 minutes, you are already too late.")));
}
/**
* We can use the $currentCycle argument to balance workload,
* or to execute low-priority actions every X cycle, or to
* only make costly actions every X cycle
*/
if($currentCycle % 3 == 0){
$this->events->push(new Event("debug", Array("info" => "I am sent every 3 cycles")));
}
}
/**
* This function will be called each cycle after $this->cycle()
* This function must return a SSEServer\Events object containing all events (SSEServer\Event) which
* should be sent as output in the current cycle.
*/
public function getEvents(): Events{
return $this->events->flush();
}
/**
* This function will be called each cycle after $this->getEvents()
* Cleanup logic should be implemented here, e.g. unset variables stored
* in this object which won't be needed at next cycle. In this case we left it blank.
*/
public function cleanUp(): Void{
}
}
//Create a new instance of our custom SSEControllerInterface object
$controller = new ExampleController();
//Create a new SSEServer instance, pass the controller object as the first arg
//Second arg is degubmode, if set to true, the server will send events with type "debug"
//if there are any.
$server = new SSEServer($controller, true);
//Every $cycleTime seconds the server will do a cycle (default is 1 seconds)
$cycleTime = 1;
$server->setCycleTime($cycleTime);
//In every $pingInterval (2nd) cycle a ping event will be sent
//containing current time (milliseconds since epoch as int) (default is every cycle (1))
$pingInterval = 2;
$server->setPingInterval(1);
//Run the server (loop)
$server->run();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.