The websocket protocol is a new network protocol based on TCP, which realizes full-duplex communication between the browser and the server - allowing the server to actively send information to the client
Why use websocket
HTTP communication can only be initiated by the client, such as ajax polling.
websocket features:
1. Built on the TCP protocol
2. Small performance overhead and efficient communication
3. The client can communicate with any server
4. The protocol identifier WS WSS is a concept with HTTPS
5. The persistent network communication protocol server actively pushes messages to the client
<?php $server = new swoole_websocket_server( "0.0.0.0" , 9504 ) ; // Set the WebSocket sub-protocol. After setting, the Http header of the handshake response will add Sec-WebSocket-Protocol: {$websocket_subprotocol} //$server->set([ // 'websocket_subprotocol' => 'chat', //]); //---- ------------ // Listen to websocket connection open event //$server->on('open', function (swoole_websocket_server $server, $request) { // echo "server: handshake success with fd{$request->fd}\n"; //}); // You can also use the following syntax $server -> 'open' , 'onOpen' ) ; function onOpen ( $server , $request ){ // Which client print_r( $request -> fd ) ; } //-------------- - // Listen to message events $server -> on ( 'message' , function (swoole_websocket_server $server , $frame ) { echo "receive from { $frame -> fd } : { $frame -> data } ,opcode: { $frame -> opcode } ,fin: { $frame -> finish } \n " ; // Push data to the websocket client connection // $fd ID of the client connection , if the specified $fd corresponds to If the TCP connection is not a websocket client, it will fail to send //$data the data content to be sent //$opcode , specify the format of the sent data content, the default is text. To send binary content $opcode parameter needs to be set to WEBSOCKET_OPCODE_BINARY // Send Returns true on success, false when sending fails // function swoole_websocket_server->push(int $fd, string $data, int $opcode = 1, bool $finish = true); $server -> push ( $frame -> fd , "this is server" ) ; }) ; // You can also write $server -> on ( 'close' , function ( $ser , $fd ) { echo "client { $fd } closed \n " ; }) ; //$server->on('request', function (swoole_http_request $request, swoole_http_response $response) { // global $server;//调用外部的server // // $server->connections 遍历所有websocket连接用户的fd,给所有用户推送 // foreach ($server->connections as $fd) { // $server->push($fd, $request->get['message']); // } //}); $server->start(); //class WebsocketTest { // public $server; // public function __construct() { // $this->server = new swoole_websocket_server("0.0.0.0", 9501); // $this->server->on('open', function (swoole_websocket_server $server, $request) { // echo "server: handshake success with fd{$request->fd}\n"; // }); // $this->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"); // }); // $this->server->on('close', function ($ser, $fd) { // echo "client {$fd} closed\n"; // }); // $this->server->on('request', function ($request, $response) { // // 接收http请求从get获取message参数的值,给用户推送 // // $this->server->connections 遍历所有websocket连接用户的fd,给所有用户推送 // foreach ($this->server->connections as $fd) { // $this->server->push($fd, $request->get['message']); // } // }); // $this->server->start(); // } //} //new WebsocketTest();
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>ws test</h1> <script> var wsUrl = 'ws://test.swoole.com:9504'; var webSocket = new WebSocket(wsUrl); //实例对象的onopen的属性 webSocket.onopen = function (p1) { console.log('con-swoole-succ'); } //实例化 onmessage webSocket.onmessage = function (p1) { console.log('ws-server-return-data:'+ p1.data); } //onclose webSocket.onclose = function (p1) { console.log('close'); } webSocket.onerror = function (p1) { console.log('error-data:'+ p1.data); } </script> </body> </html>