socket.php
<?PHP//creating WebSocket server objects, listening on 0.0.0.0:9502 ports$ws =NewSwoole_websocket_server ("0.0.0.0",9503);//Monitoring WebSocket Connection Open Event$ws->on ('Open', function ($ws, $request) {var_dump ($request->FD, $requestGet, $requestserver); $ws->push ($request->fd,"Hello, welcome\n.");});//listening for WebSocket message events$ws->on ('message', function ($ws, $frame) {echo"Message: {$frame->data}\n"; $ws->push ($frame->fd,"server: {$frame->data}");});//monitoring WebSocket connection shutdown Events$ws->on ('Close', function ($ws, $FD) {echo"client-{$FD} is closed\n";}); $ws->start ();
Socket.html
<! DOCTYPE html>"UTF-8"> <title>socket</title>123</body><script>varWSServer ='ws://192.168.70.167:9503'; varWebSocket =NewWebSocket (WSServer); Websocket.onopen=function (evt) {Console.log ("Connected to WebSocket server."); }; Websocket.onclose=function (evt) {Console.log ("Disconnected"); }; Websocket.onmessage=function (evt) {Console.log ('retrieved data from server:'+evt.data); }; Websocket.onerror=function (EVT, E) {Console.log ('Error occured:'+evt.data); };</script>socket.php,socket.html Directory/usr/local/nginx/html/
Start socket.php:
# php socket.php
Start socket.html
Browser Open: 192.168.70.167/socket.html
Swoole WebSocket Service