日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

swoole websocket服务

發(fā)布時(shí)間:2025/5/22 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 swoole websocket服务 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

websocket協(xié)議是基于TCP的一種新網(wǎng)絡(luò)協(xié)議,它實(shí)現(xiàn)了瀏覽器與服務(wù)器全雙工(full-duplex)通信--可以讓服務(wù)器主動(dòng)發(fā)送信息給客戶端

為什么用websocket

HTTP的通信只能由客戶端發(fā)起,比如ajax輪尋啊

websocket特點(diǎn):

1.建立在TCP協(xié)議之上

2.性能開銷小通信高效

3.客戶端可以與任意服務(wù)器通信

4.協(xié)議標(biāo)識(shí)符WS WSS跟HTTPS一個(gè)概念

5.持久化網(wǎng)絡(luò)通信協(xié)議 服務(wù)器主動(dòng)向客戶端推送消息

<?php $server = new swoole_websocket_server("0.0.0.0", 9504); //設(shè)置WebSocket子協(xié)議。設(shè)置后握手響應(yīng)的Http頭會(huì)增加Sec-WebSocket-Protocol: {$websocket_subprotocol} //$server->set([ // 'websocket_subprotocol' => 'chat', //]); //---------------- //監(jiān)聽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){ //哪個(gè)客戶端 print_r($request->fd); } //--------------- //監(jiān)聽消息事件 $server->on('message', function (swoole_websocket_server $server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; //websocket客戶端連接推送數(shù)據(jù) // $fd 客戶端連接的ID,如果指定的$fd對(duì)應(yīng)的TCP連接并非websocket客戶端,將會(huì)發(fā)送失敗 //$data 要發(fā)送的數(shù)據(jù)內(nèi)容 //$opcode,指定發(fā)送數(shù)據(jù)內(nèi)容的格式,默認(rèn)為文本。發(fā)送二進(jìn)制內(nèi)容$opcode參數(shù)需要設(shè)置為WEBSOCKET_OPCODE_BINARY //發(fā)送成功返回true,發(fā)送失敗返回false // function swoole_websocket_server->push(int $fd, string $data, int $opcode = 1, bool $finish = true); $server->push($frame->fd, "this is server"); }); //也可以用以上單獨(dú)方法去寫 $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;//調(diào)用外部的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請(qǐng)求從get獲取message參數(shù)的值,給用戶推送 // // $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); //實(shí)例對(duì)象的onopen的屬性 webSocket.onopen = function (p1) {//發(fā)服務(wù)器發(fā)數(shù)據(jù) webSocket.send('hello ws'); console.log('con-swoole-succ'); }; //實(shí)例化 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>

瀏覽器如果報(bào)以下錯(cuò)誤

ws.html:11 WebSocket connection to 'ws://10.200.9.221:9504/' failed: Error i

請(qǐng)查看一下你的防火墻,是否開放此端口





<?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(); } /** * 監(jiān)聽websocke連接打開事件 * @param $server * @param $request */ function onOpen($server, $request) { print_r($request->fd); } /** * 監(jiān)聽消息事件 * websocket客戶端連接推送數(shù)據(jù) * $fd 客戶端連接的ID,如果指定的$fd對(duì)應(yīng)的TCP連接并非websocket客戶端,將會(huì)發(fā)送失敗 * $data 要發(fā)送的數(shù)據(jù)內(nèi)容 * $opcode,指定發(fā)送數(shù)據(jù)內(nèi)容的格式,默認(rèn)為文本。發(fā)送二進(jìn)制內(nèi)容$opcode參數(shù)需要設(shè)置為WEBSOCKET_OPCODE_BINARY * 發(fā)送成功返回true,發(fā)送失敗返回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"); } /** * 關(guān)閉 * @param $ws * @param $fd */ function onClose($ws, $fd) { echo "client {$fd} closed\n"; } } $ws = new wsServer();

總結(jié)

以上是生活随笔為你收集整理的swoole websocket服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。