How to use PHP asynchronous parallel network extension swoole

Source: Internet
Author: User
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.

?

?

?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.