Open Swoole HTTP Server Example

<?php use Swoole\Http\Server; use Swoole\Http\Request; use Swoole\Http\Response; $server = new Swoole\HTTP\Server("127.0.0.1", 9501); $server->on("Start", function(Server $server) { echo "Swoole http server is started at http://127.0.0.1:9501\n"; }); $server->on("Request", function(Request $request, Response $response) { $response->header("Content-Type", "text/plain"); $response->end("Hello World\n"); }); $server->start();

Open Swoole TCP Server Example

<?php $server = new Swoole\Server("127.0.0.1", 9501); $server->on('connect', function ($server, $fd){ echo "New connection established: #{$fd}.\n"; }); $server->on('receive', function ($server, $fd, $from_id, $data) { $server->send($fd, "Echo to #{$fd}: \n".$data); $server->close($fd); }); $server->on('close', function ($server, $fd) { echo "Connection closed: #{$fd}.\n"; }); $server->start();

Open Swoole WebSocket Server Example

<?php use Swoole\WebSocket\Server; use Swoole\Http\Request; use Swoole\WebSocket\Frame; $server = new Server("127.0.0.1", 9501); $server->on("Start", function(Server $server) { echo "Swoole WebSocket Server started at 127.0.0.1:9501\n"; }); $server->on('Open', function(Server $server, Swoole\Http\Request $request) { echo "Connection open: {$request->fd}\n"; $server->tick(1000, function() use ($server, $request) { $server->push($request->fd, json_encode(["hello", time()])); }); }); $server->on('Message', function(Server $server, Frame $frame) { echo "Received message: {$frame->data}\n"; $server->push($frame->fd, json_encode(["hello", time()])); }); $server->on('Close', function(Server $server, int $fd) { echo "Connection close: {$fd}\n"; }); $server->on('Disconnect', function(Server $server, int $fd) { echo "Connection disconnect: {$fd}\n"; }); $server->start();