首页
开发手册
应用中心
工具
用户中心
Swoole Set Up the Environment(websocket)
English
中文简体
English
上一篇
下一篇
#### What is Swoole? Swoole enables PHP developers to write high-performance and high-concurrency services such as TCP, UDP, Unix Socket, HTTP, and WebSocket, so that PHP is no longer limited to the Web domain. The maturity of the Swoole4 coroutine brings PHP into an unprecedented period, providing a unique possibility for performance improvement. Swoole can be widely used in the Internet, mobile communications, cloud computing, online games, Internet of Things (IOT), Internet of Vehicles, smart home and other fields. Comprehensive performance comparison FoundPHP has officially adapted Swoole to ensure that users avoid various pits during development and enjoy the development process. #### How to Install Swoole We only explain Linux here. Under Linux, we need to choose from the pagoda software store **PHP7**.The above version is Swoole4, here we have installed the php7.4 version (it is recommended to install Apache and Mysql). ![](data/files/edit/20/03/24/200324_14072628.png) After the installation is successful, click Settings, select Install Extension in the new window, find swoole4, click Install, and confirm. ![](data/files/edit/20/03/24/200324_14134636.png) After the installation is successful, check the phpinfo() information to know whether the installation is successful. ![](data/files/edit/20/03/24/200324_14165338.png) After success, press Ctrl+f to search for swoole and you will find ![](data/files/edit/20/03/24/200324_14235381.png) So far it has been successfully installed. If it cannot be used, please check whether the firewall has opened port 9503 or your customized port. ------------ #### How to Use Swoole Linux requires commands, we recommend **winscp**+**putty**,Enter the linux address, account and password to access the command line. Here we take the pagoda as an example to find the project directory,Usually in the corresponding website under the .**/www/wwwroot**directory. We create a simple server-side demo, copy and save the code to ws.php and put it in the server's directory. ws.php ```php on('open', function ($ws, $request) { var_dump($request->fd, $request->get, $request->server); $ws->push($request->fd, "Welcome FoundPHP IM\n"); }); //listen for message events $ws->on('message', function ($ws, $frame) { echo "Message: {$frame->data}\n"; $ws->push($frame->fd, "server: {$frame->data}"); }); //listen for connection close events $ws->on('close', function ($ws, $fd) { echo "client is closed\n"; }); //Start service $ws->start(); ?> ``` Enter at the command line: ` php ws.php` After execution, a state similar to the card machine appears to indicate successful operation. ![](data/files/edit/20/03/24/200324_14327293.png) The configuration of the server is basically completed.
#### Swoole Basic Functions Create a websocket server object, you can use 0.0.0.0 to monitor this machine, port 9503, if your server uses other ports, you can also set it to the same. `$ws = new Swoole\WebSocket\Server("0.0.0.0", 9503);` ------------
**Monitor WebSocket connection open event** `open` Here we use it as a handshake, and return a project identifier Welcome FoundPHP when the client requests. **Instructions:** ```php: $ws->on('open',function ($ws, $fd) { //logic processing code }); ``` ------------
**Listen for WebSocket messages**
This is the core function of development `message` **Instructions:** ```php: $ws->on('message',function ($ws, $frame) { //logic processing code }); ``` ------------
**Listen to the WebSocket Connection Close Event** `close` Mainly used to capture users who exit or close unexpectedly **Instructions:** ```php: $ws->on('close',function ($ws, $fd) { //logic processing code }); ``` ------------
**Start websocket Service** ```php: $ws->start(); ``` This is the most important thing. Monitoring will not be executed if it is not started.