WS tick interval, WS manager able to limit connections per session

This commit is contained in:
fbsanchez 2019-10-24 10:35:16 +02:00
parent 709f4cac81
commit d34aa0917b
2 changed files with 32 additions and 3 deletions

View File

@ -130,6 +130,13 @@ class WSManager extends WebSocketServer
*/
public $handlerTick = [];
/**
* Allow only one connection per user session.
*
* @var boolean
*/
public $socketPerSession = false;
/**
* Builder.
@ -292,8 +299,10 @@ class WSManager extends WebSocketServer
);
$this->stderr('ONLINE '.$user->address.'('.$user->account->idUser.')');
// Disconnect previous sessions.
$this->cleanupSocketByCookie($user);
if ($this->socketPerSession === true) {
// Disconnect previous sessions.
$this->cleanupSocketByCookie($user);
}
// Launch registered handler.
$this->callHandler(

View File

@ -164,6 +164,21 @@ abstract class WebSocketServer
*/
public $timeout = 1;
/**
* Do not call tick every iteration, use a minimum time lapse.
* Measure: seconds.
*
* @var integer
*/
public $tickInterval = 1;
/**
* Last tick call. (unix timestamp).
*
* @var integer
*/
public $lastTickTimestamp = 0;
/**
* Builder.
@ -420,7 +435,12 @@ abstract class WebSocketServer
$except = null;
$write = null;
$this->pTick();
$this->tick();
if ((time() - $this->lastTickTimestamp) > $this->tickInterval) {
$this->lastTickTimestamp = time();
$this->tick();
}
socket_select($read, $write, $except, 0, $this->timeout);
foreach ($read as $socket) {
if ($socket == $this->master) {