When swoole meets thinkphp5: Hello, world!

Time:2021-7-25

When swoole meets thinkphp5: Hello, world!


This article assumes that you already have the PHP environment of Linux operating system. It is strongly recommended to use vagrant to build the development environment

Installing the swoole PECL extension

You can use the PECL command or compile and install the source package. This article uses the PECL command to install swoole
Enter at the command line

$ > pecl install swoole

Install the swoole PECL extension, and execute the command throughphp -mCommand to check whether the swoole is successfully installed. If the returned extension list containsswooleIt indicates that the installation has been successful. You can also use thephpinfoTo check whether the swoole installation was successful.

Install thinkphp5 frame

1. Install composer

If composer is already installed, you can skip this step, but be sure to passcomposer self-updateCommand to ensure that the latest version of composer has been used. Use the following command to download composer.phar directly from the official website of composer and automatically install it into the/usr/local/bin/Directory

$ > php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/local/bin/ --filename=composer
$ > composer self-update

If the above installation process is extremely slow, you can try to install it through the composer domestic image in the following way.

$ > php -r "readfile('http://install.phpcomposer.com/installer');" | php -- --install-dir=/usr/local/bin/ --filename=composer
$ > composer config -g repo.packagist composer https://packagist.phpcomposer.com
$ > composer self-update

2. Install thinkphp5 frame

Use the following command to install the latest version of thinkphp5 framework into the thinksoolefirst folder in the current directory

$ > composer create-project topthink/think thinkSwooleFirst  --prefer-dist

Install the think spool composer package

Switch the command line directory to thinksoolefirst directory, and install thinksoole through composer

$ > composer require topthink/think-swoole

Swoole PECL extension must be installed before installing the think swoole composer package

HelloSwoole

When the environment is all set up, we willapplication/index/controllerCreate under directoryDemon.phpFile to write the Hello world of the TCP server of swoole and thinkphp5

<?php
namespace app\index\controller;

//You must use and inherit the \ think \ spool \ server class
use think\swoole\Server;

class Demon extends Server
{
    //Listen to all addresses
    protected $host = '0.0.0.0';
    //Listen to port 9501
    protected $port = 9501;
    //Specifies that the running mode is multi process
    protected $mode = SWOOLE_PROCESS;
    //Specifies a TCP socket of type IPv4
    protected $sockType = SWOOLE_SOCK_TCP;
    //Configuration item
    protected $option = [
        /** 
         *Set the number of worker processes started
         *The business code is fully asynchronous and non blocking. It is most reasonable to set it to 1-4 times the CPU
         *The service code is synchronous blocking, which needs to be adjusted according to the request response time and system load
         */
        'worker_num' => 4,
        //Daemon
        'daemonize'  => true,
        //Length of listening queue
        'backlog'    => 128
    ];

    /**
     *Callback function when message is received
     * @param \swoole_ server $serv swoole_ Server object
     *@ param $FD file descriptor of TCP client connection
     * @param $from_ ID ID of the reactor thread where the TCP connection is located
     *@ param $data received data content
     */
    public function onReceive(\swoole_server $server, $fd, $from_id, $data)
    {
        $server->send($fd, 'onReceive: ' . $data);
    }
}

The above code completes a relatively simple TCP server using thinkphp5 and spool, and then you can run the server
Use the following command to start the TCP server

$ > php public/index.php index/Demon/start

We can also modify the configuration itemdeamonizeIt is false, so that the program does not use the daemon, which is more convenient for our debugging

We can also use the daemon methodlsofTool to check whether the port is in normal state

$ > lsof -i:9501

After confirming the normal operation of the program, usetelnetTool to connect to the TCP server. In a later article, we will also talk about how to use the TCP client of swoole to connect to the TCP server

telnet 127.0.0.1 9501

inputhelloThe following results will be obtained

onReceive: hello

For more complete examples of swoole and thinkphp5, please visit my new bookWhen swoole meets thinkphp5