May 14, 2021 Swoole
swoole-1.7.9 adds built-in websocket server support to write an asynchronous non-blocking multi-process WebSocket server in a few lines of PHP code.
Common usage scenarios: When we're using php development, the worst thing to use natively is the socket class library, and in developing IM and timely communication projects, we now have a new option for back-end use php Swoole WebSocket and front-end html5 WebSocket;
$server = new swoole_websocket_server("0.0.0.0", 9501);
$server->on('open', function (swoole_websocket_server $server, $request) {
echo "server: handshake success with fd{$request->fd}\n";
});
$server->on('message', function (swoole_websocket_server $server, $frame) {
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "this is server");
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
$server->start();
swoole_websocket_server inherited from swoole_http_server
http 400
http request
Swoole\Http\Client
as a WebSocket client
You can even use a combination socket.io with development