swoole websocket服务
生活随笔
收集整理的這篇文章主要介紹了
swoole websocket服务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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
請查看一下你的防火墻,是否開放此端口
總結
以上是生活随笔為你收集整理的swoole websocket服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10怎么设置登录用户名和密码忘了怎
- 下一篇: JAVA test代码运行