PHP asynchronous parallel network expansion how swoole uses Swoole is PHP's asynchronous parallel expansion, a bit like Node. js, but swoole supports both synchronous and asynchronous, more powerful than node. The Swoole extension is based on epoll high-performance event polling and is multi-threaded, with excellent performance .? In addition to the asynchronous parallel network that provides the network to Serve PHP, Swoole expands how to use swoole.
Swoole is a PHP asynchronous parallel extension, a bit like Node. js, but swoole supports both synchronization and asynchronization, which is more powerful than node. The Swoole extension is based on epoll high-performance event polling and is multi-threaded, with excellent performance.
?
In addition to the network Server/Client function, Swoole also provides the Task asynchronous Task manager, which enables some slow functions in your program to be executed asynchronously.
?
This article describes how to use swoole extensions.
?
Step 1 install
Swoole only supports Linux, FreeBSD, and Mac. if you are using a Windows system, install a virtual machine, either VirtualBox or VMWare. Then install Linux in the virtual machine.
?
Swoole has been added to the official PHP Extension library, so you only need to execute
?
pecl install swoole
?
You can install it. After installation, modify php. ini and add extension = swoole. so. Check php-m or phpinfo. if swoole is displayed, the installation is successful.
?
Step 2 write the Server program
Create a PHP script file server. php with the following code:
?
$serv = new swoole_server("127.0.0.1", 9501);$serv->on('connect', function ($serv, $fd){ echo "Client:Connect.\n";});$serv->on('receive', function ($serv, $fd, $from_id, $data) { $serv->send($fd, 'Swoole: '.$data);});$serv->on('close', function ($serv, $fd) { echo "Client: Close.\n";});$serv->start();
? Then execute:
?
php server.php
?
? Use telnet to connect to your server:
telnet 127.0.0.1 9501> hello world> Swoole: hello world
?
? Is it very simple? just 11 lines of code creates an asynchronous high-concurrency TCPServer for data sending and receiving through the underlying network. Next, you can do what you want to do, instant chat, file sending and receiving, communication, and so on.
?
?
?