* @param string $reason The reason for the close (may be empty) * @return mixed */ public function onClose(int $clientId, int $code, string $reason) { print "client quit\n"; } /** * Invoked when the server is stopping * * If the application initialized resources in Websocket::onStart() this is the * place to free them. * * There is no need to call Endpoint::close() in this method because the * websocket endpoint automatically closes all connected clients before this application * method is ever invoked. * * If this method is a Generator it will be resolved as a coroutine before the server * is allowed to fully shutdown. Additionally, if this method returns a Promise the * server will not shutdown until that promise resolves. * * @return mixed */ public function onStop() { } } $router = router()->route("GET", "/", function (Request $req, Response $res) { $res->send("<html><body><h1>Hello, world.</h1></body></html>"); })->route("GET", "/ws", websocket(new CMS())); (new Host())->expose("*", 8005)->use($router);
} } } } else { if (isset($_REQUEST) && isset($_REQUEST['port'])) { $port = (int) $_REQUEST['port']; } } // ============================================================================= $database = pg_connect("dbname=cadist3d_db user=cadist3d"); pg_set_error_verbosity($database, PGSQL_ERRORS_VERBOSE); // ============================================================================= // get algorithm weights from database use_algorithm_weights(get_algorithm_weights()); // ============================================================================= $websocket = websocket(); websocket_set_port($websocket, $port); websocket_set_message_limit($websocket, $maxMessageSize); websocket_on_client($websocket, 'handle_client'); websocket_on_message($websocket, 'handle_message'); websocket_on_close($websocket, 'handle_close'); console("===\nServer started on port {$port}\n\n"); websocket_run($websocket); if (isset($websocket['error'])) { console("=## Error: " . $websocket['error']); } pg_close($database); console("==="); $console_enabled = TRUE; console("Server terminated"); // =============================================================================