websocket协议是基于TCP的一种新网络协议,它实现了浏览器与服务器全双工(full-duplex)通信--可以让服务器主动发送信息给客户端
为什么用websocket
HTTP的通信只能由客户端发起,比如ajax轮寻啊
websocket特点:
1.建立在TCP协议之上
2.性能开销小通信高效
3.客户端可以与任意服务器通信
4.协议标识符WS WSS跟HTTPS一个概念
5.持久化网络通信协议 服务器主动向客户端推送消息
<?php $server = new swoole_websocket_server("0.0.0.0", 9504); //设置WebSocket子协议。设置后握手响应的Http头会增加Sec-WebSocket-Protocol: {$websocket_subprotocol} //$server->set([ // 'websocket_subprotocol' => 'chat', //]); //---------------- //监听websocke连接打开事件 //$server->on('open', function (swoole_websocket_server $server, $request) { // echo "server: handshake success with fd{$request->fd}\n"; //}); //也可以用以下的写法 $server->on('open', 'onOpen'); function onOpen($server, $request){ //哪个客户端 print_r($request->fd); } //--------------- //监听消息事件 $server->on('message', function (swoole_websocket_server $server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; //向websocket客户端连接推送数据 // $fd 客户端连接的ID,如果指定的$fd对应的TCP连接并非websocket客户端,将会发送失败 //$data 要发送的数据内容 //$opcode,指定发送数据内容的格式,默认为文本。发送二进制内容$opcode参数需要设置为WEBSOCKET_OPCODE_BINARY //发送成功返回true,发送失败返回false // function swoole_websocket_server->push(int $fd, string $data, int $opcode = 1, bool $finish = true); $server->push($frame->fd, "this is server"); }); //也可以用以上单独方法去写 $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://10.200.9.221:9504'; var webSocket = new WebSocket(wsUrl); //实例对象的onopen的属性 webSocket.onopen = function (p1) { //发服务器发数据 webSocket.send('hello ws'); 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>
浏览器如果报以下错误
ws.html:11 WebSocket connection to 'ws://10.200.9.221:9504/' failed: Error i
请查看一下你的防火墙,是否开放此端口
<?php class wsServer { const Host = '0.0.0.0'; const PORT = '9504'; public $ws = null; function __construct() { $this->ws = new swoole_websocket_server(self::Host, self::PORT); $this->ws->on('open', [$this, 'onOpen']); $this->ws->on('message', [$this, 'onMessage']); $this->ws->on('close', [$this, 'onClose']); $this->ws->start(); } /** * 监听websocke连接打开事件 * @param $server * @param $request */ function onOpen($server, $request) { print_r($request->fd); } /** * 监听消息事件 * 向websocket客户端连接推送数据 * $fd 客户端连接的ID,如果指定的$fd对应的TCP连接并非websocket客户端,将会发送失败 * $data 要发送的数据内容 * $opcode,指定发送数据内容的格式,默认为文本。发送二进制内容$opcode参数需要设置为WEBSOCKET_OPCODE_BINARY * 发送成功返回true,发送失败返回false * function swoole_websocket_server->push(int $fd, string $data, int $opcode = 1, bool $finish = true); * @param $ws * @param $frame */ function onMessage($ws, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; $ws->push($frame->fd, "this is server"); } /** * 关闭 * @param $ws * @param $fd */ function onClose($ws, $fd) { echo "client {$fd} closed\n"; } } $ws = new wsServer();