CentOS installs php extension swoole and uses case study (applicable to php7)
Introduction First of all, centOS has installed the php environment, and the swoole project has been included in the official PHP extension library. In addition to downloading and compiling manually, you can also download and install swoole with one click through the pecl command provided by the official PHP.
Swoole official website: https://www.swoole.com/
Official explanation:
Swoole: PHP asynchronous network communication engine for production environments
Enables PHP developers to write high-performance asynchronous concurrent TCP, UDP, Unix Socket, HTTP, WebSocket services. Swoole can be widely used in the Internet, mobile communication, enterprise software, cloud computing, online games, Internet of Things (IOT), Internet of Vehicles, smart home and other fields. Using PHP + Swoole as a network communication framework can greatly improve the efficiency of enterprise IT R&D teams and focus more on developing innovative products.
1. Install swoole using the pecl command officially provided by PHP
First, centOS has installed the php environment
The swoole project has been included in the official PHP extension library. In addition to downloading and compiling manually, you can also download and install swoole with one click through the pecl command provided by PHP.
1 | pecl install swoole |
Second, configure php.ini
After successful compilation and installation, modify php.ini to add
1 | extension=swoole.so |
Use php -m or phpinfo() to check whether swoole is successfully loaded. If there is no possibility that the path of php.ini is wrong, you can use php --ini to locate the absolute path of php.ini.
3. Restart the service
1 2 | service php-fpm restart service nginx restart |
Fourth, code testing
Server-side server.php code
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php
$server = new swoole_server("192.168.1.160", 55152); $server->on('connect', function ($server, $fd){ echo "Client:Connect.\n"; }); $server->on('receive', function ($server, $fd, $from_id, $data) { $server->send($fd, 'Here is Swoole: '.$data); }); $server->on('close', function ($server, $fd) { echo "Client: Close.\n"; }); $server->start(); |
Client client.php code
1 2 3 4 5 6 7 8 9 10 11 | <?php
$client = new swoole_client(SWOOLE_SOCK_TCP); if (!$client->connect('192.168.1.160', 55152, -1)) { exit("connect failed. Error: {$client->errCode}\n"); } $client->send("hello world\n"); echo $client->recv(); $client->close(); |
Five, test the above two php codes
1. Start the service
1 | [root@localhost ~]# php server.php |
2. Linux terminal telnet test
If you encounter -bash: telnet: command not found
Install telnet using the following command
yum list telnet* # View telnet related installation packages
yum install telnet-server # install telnet service
yum install telnet.* # install telnet client
From the figure below, you can see that you can communicate with the server by issuing a session
Browser client test
0 Comments