public function bind()
{
$err = $errno = 0;
$this->FLASH_POLICY_FILE = str_replace('to-ports="*', 'to-ports="' . $this->uri->getPort() ?: 80, $this->FLASH_POLICY_FILE);
$serverSocket = stream_socket_server($this->uri->toString(), $errno, $err, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $this->_context);
$this->_logger->notice(sprintf("phpws listening on %s", $this->uri->toString()));
if ($serverSocket == false) {
$this->_logger->err("Error: {$err}");
return;
}
$timeOut =& $this->purgeUserTimeOut;
$sockets = $this->_streams;
$that = $this;
$logger = $this->_logger;
$this->loop->addReadStream($serverSocket, function ($serverSocket) use($that, $logger, $sockets) {
try {
$newSocket = stream_socket_accept($serverSocket);
} catch (\ErrorException $e) {
$newSocket = false;
}
if (false === $newSocket) {
return;
}
stream_set_blocking($newSocket, 0);
$client = new WebSocketConnection($newSocket, $that->loop, $logger);
$sockets->attach($client);
$client->on("handshake", function (Handshake $request) use($that, $client) {
$that->emit("handshake", array($client->getTransport(), $request));
});
$client->on("connect", function () use($that, $client, $logger) {
$con = $client->getTransport();
$that->getConnections()->attach($con);
$that->emit("connect", array("client" => $con));
});
$client->on("message", function ($message) use($that, $client, $logger) {
$connection = $client->getTransport();
$that->emit("message", array("client" => $connection, "message" => $message));
});
$client->on("close", function () use($that, $client, $logger, &$sockets, $client) {
$sockets->detach($client);
$connection = $client->getTransport();
if ($connection) {
$that->getConnections()->detach($connection);
$that->emit("disconnect", array("client" => $connection));
}
});
$client->on("flashXmlRequest", function () use($that, $client) {
$client->getTransport()->sendString($that->FLASH_POLICY_FILE);
$client->close();
});
});
$this->loop->addPeriodicTimer(5, function () use($timeOut, $sockets, $that) {
# Lets send some pings
foreach ($that->getConnections() as $c) {
if ($c instanceof WebSocketTransportHybi) {
$c->sendFrame(WebSocketFrame::create(WebSocketOpcode::PingFrame));
}
}
$currentTime = time();
if ($timeOut == null) {
return;
}
foreach ($sockets as $s) {
if ($currentTime - $s->getLastChanged() > $timeOut) {
$s->close();
}
}
});
}