Home > Article > PHP Framework > Show swoole's websocket connection
The editor is also new to swoole, and the official document provides very little information for the swoole demo, and some places are not clearly explained. After a lot of tossing, the websocket finally shook hands in two days. Success, write down my experience, hope it can give some help to those in need.
First of all, let me introduce that my running environment is placed directly on the external network server. I will not go into details about the program running environment. You can refer to the swoole official website. I access it directly through IP. Among them, the small The editor encountered a pitfall, that is, the port we let the HTTP server or websocket monitor must be opened on the server. The editor's Alibaba Cloud server needs to go to the security group to set the release port, and the inbound and outbound directions must be set. Otherwise you will find that you cannot access the server through ip: port
Recommended (free): swoole
Here I will introduce the next two A way to connect to the websocket server
1. Connect to our websocket server by accessing the http server and then accessing our html page, so we need 3 files. 1 http.php 2 ws.php 3 ws.html Simply paste my picture, which is also written with reference to the official documentation
http.php
<?php /** * Created by PhpStorm. * User: xyj * Date: 18-9-9 * Time: 下午4:31 */ //实例化 $http_server = new swoole_http_server('0.0.0.0',9501); //服务器配置 $http_server->set( [ 'enable_static_handler' => true, 'document_root' => '/www/wwwroot/test_project/swoole', ] ); $http_server->on('request',function($request ,$response){ //print_r($request->get); //设置响应头信息 $response->cookie('xyj','hello',86400); //服务器返回信息 $response->end('http_server' . json_encode($request->get)); }); $http_server->start();
ws.php
<?php /** * Created by PhpStorm. * User: xyj * Date: 18-9-9 * Time: 下午5:02 */ //创建websocket服务器对象,监听0.0.0.0:9502端口 $ws = new swoole_websocket_server("0.0.0.0", 9502); $ws->set( [ 'enable_static_handler' => true, 'document_root' => '/www/wwwroot/test_project/swoole', ] ); //监听WebSocket连接打开事件 $ws->on('open', function ($ws, $request) { var_dump($request->fd, $request->get, $request->server); $ws->push($request->fd, "hello, welcome\n"); }); //监听WebSocket消息事件 $ws->on('message', function ($ws, $frame) { echo "Message: {$frame->data}\n"; $ws->push($frame->fd, "server: {$frame->data}"); }); //监听WebSocket连接关闭事件 $ws->on('close', function ($ws, $fd) { echo "client-{$fd} is closed\n"; }); $ws->start();
ws.html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h1>hello swoole 测试</h1> </body> </html> <script> var wsServer = 'ws://:9502'; var websocket = new WebSocket(wsServer); websocket.onopen = function (evt) { websocket.send('hello swoole!!!'); 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>
If it is running in a local test environment, then wsServer = 'ws://127.0.0.1:9502'. If it is an external network, then 127.0.0.1 must be changed to the one on your browser. The accessed address, the port here must be consistent with your websocket server port, otherwise it will be inaccessible
Now that the preparations are done, we will start accessing the websocket. First, run http.php and ws.php in the terminal. Under normal circumstances, this is the case. If you encounter an error, the common problem is that the port is occupied. If the swoole extension is not installed or there is a syntax error, then please read the official documentation carefully.
If the port is occupied, we can use the lsof -i:9501 command to view the port information. Before killing, please carefully check whether the port is an important port. Do not kill blindly.
Another way is that we have opened http.php and the ports that may be occupied after closing are not released in time. When we php http.php again, it will also throw an error that the port is occupied. Here We can use this command netstat -ntlp to view the tcp listening port. Kill its parent process and then run it and it will be ok. Continue to use this command to view the tcp port as shown
Come here our two servers have been started. We can access it through the browser. Enter the address on the browser: 9501. At this time, the port here must be the same as the http server port instead of the websocket port. If the access is successful, the following figure will appear
This is how I understand these messages. If there is any mistake, please correct me. At this time, our websocket handshake is successful
Regarding the second type of access, we only open ws Accessing .php on the browser is the same as the first type, but the port at this time will become the websocket port, and the display page is the same as the first type.
Regarding linking websocket, the editor's process is roughly like this. If there is anything wrong, you are welcome to come and correct me.
The above is the detailed content of Show swoole's websocket connection. For more information, please follow other related articles on the PHP Chinese website!