Update WsServer.php
This commit is contained in:
parent
e2b7a8f95a
commit
8bbc516f5e
@ -61,22 +61,12 @@ class WsServer implements HttpServerInterface {
|
||||
*/
|
||||
private $msgCb;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $keepAliveEnabled;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $keepAliveTimeout = 30;
|
||||
|
||||
/**
|
||||
* @param \Ratchet\WebSocket\MessageComponentInterface|\Ratchet\MessageComponentInterface $component Your application to run with WebSockets
|
||||
* @param int $keepAliveTimeout
|
||||
* @note If you want to enable sub-protocols have your component implement WsServerInterface as well
|
||||
*/
|
||||
public function __construct(ComponentInterface $component, $keepAliveTimeout = 30) {
|
||||
public function __construct(ComponentInterface $component) {
|
||||
if ($component instanceof MessageComponentInterface) {
|
||||
$this->msgCb = function(ConnectionInterface $conn, MessageInterface $msg) {
|
||||
$this->delegate->onMessage($conn, $msg);
|
||||
@ -99,7 +89,6 @@ class WsServer implements HttpServerInterface {
|
||||
$this->closeFrameChecker = new CloseFrameChecker;
|
||||
$this->handshakeNegotiator = new ServerNegotiator(new RequestVerifier);
|
||||
$this->handshakeNegotiator->setStrictSubProtocolCheck(true);
|
||||
$this->keepAliveEnabled = false;
|
||||
|
||||
if ($component instanceof WsServerInterface) {
|
||||
$this->handshakeNegotiator->setSupportedSubProtocols($component->getSubProtocols());
|
||||
@ -208,10 +197,7 @@ class WsServer implements HttpServerInterface {
|
||||
$this->handshakeNegotiator->setStrictSubProtocolCheck($enable);
|
||||
}
|
||||
|
||||
public function enableKeepAlive(LoopInterface $loop) {
|
||||
if ($this->keepAliveEnabled) {
|
||||
return;
|
||||
}
|
||||
public function enableKeepAlive(LoopInterface $loop, $interval = 30) {
|
||||
$this->keepAliveEnabled = true;
|
||||
$lastPing = new Frame(uniqid(), true, Frame::OP_PING);
|
||||
$pingedConnections = new \SplObjectStorage;
|
||||
@ -223,7 +209,7 @@ class WsServer implements HttpServerInterface {
|
||||
}
|
||||
};
|
||||
|
||||
$loop->addPeriodicTimer((int)$this->keepAliveTimeout, function() use ($pingedConnections, &$lastPing, $splClearer) {
|
||||
$loop->addPeriodicTimer((int)$interval, function() use ($pingedConnections, &$lastPing, $splClearer) {
|
||||
foreach ($pingedConnections as $wsConn) {
|
||||
$wsConn->close();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user