<?php # RUN FROM COMMAND LINE!! require_once '../../websocket-php/lib/Base.php'; require_once '../../websocket-php/lib/Server.php'; require_once '../../websocket-php/lib/Exception.php'; require_once '../../websocket-php/lib/ConnectionException.php'; use WebSocket\Server; define('TIMEOUT', 200); // Setting timeout to 200 seconds to make time for all tests and manual runs. $server = new Server(array('timeout' => TIMEOUT)); echo "Server started with timeout: ", TIMEOUT, "ms — at ", date("c"); echo "\n", "port: ", $server->getPort(), "\n"; while ($connection = $server->accept()) { $test_id = $server->getPath(); $test_id = substr($test_id, 1); try { while (1) { $message = $server->receive(); echo "Received {$message}\n\n"; if ($message === 'exit') { echo microtime(true), " Client told me to quit. Bye bye.\n"; echo microtime(true), " Close response: ", $server->close(), "\n"; echo microtime(true), " Close status: ", $server->getCloseStatus(), "\n"; exit; } elseif ($auth = $server->getHeader('Authorization')) { $server->send("{$auth} - {$message}", 'text', false); } else { $server->send($message, 'text', false); } }
<?php ini_set('display_errors', 1); error_reporting(E_ALL); use WebSocket\Server; require __DIR__ . '/../vendor/autoload.php'; $server = new Server('127.0.0.1', 8000, false); // server settings: $server->setMaxClients(100); $server->setMaxConnectionsPerIp(100); $server->setMaxRequestsPerMinute(2000); $server->setCheckOrigin(false); // Hint: Status application should not be removed as it displays usefull server informations: $server->registerApplication('status', \WebSocket\Application\StatusApplication::getInstance()); $server->registerApplication('demo', \WebSocket\Application\DemoApplication::getInstance()); $server->run();
public function stop() { $this->server->stop(); }
<?php use WebSocket as W; use WebSocket\Application as WA; // autoload function function __autoload($class) { // convert namespace to full file path $class = '' . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php'; require_once $class; } $config = parse_ini_file('config.ini'); $server = new W\Server($config['address'], $config['port']); $server->registerApplication('example', WA\ExampleApplication::getInstance()); // show Connection log (connect, disconnect, data receive...) $server->setDebug($config['debug']); $server->run();