When Swoole Meets ThinkPHP5: Hello,World!

Keywords: PHP lsof Linux socket

When Swoole Meets ThinkPHP5: Hello,World!

This article assumes that you already have a PHP environment for the Linux operating system. It is highly recommended that you use Vagrant to build a development environment.

Install Swoole PECL Extension

Swoole can be installed either by pecl command or by compiling and installing source packages. This article uses pecl command to install Swoole.
Enter on the command line

$ > pecl install swoole

Install Swoole PECL extensions. After executing the command, check whether Swoole has been installed successfully by php -m command. If swoole is included in the list of extensions returned, it indicates that Swoole has been installed successfully. You can also check whether Swoole has been installed successfully by phpinfo.

Install ThinkPHP5 Framework

1. Install Composer

If you have already installed Composer, you can skip this step, but make sure that you use the composer self-update command to ensure that you have used the latest version of Composer to download Composer.phar directly from the Composer official website and install it automatically into the / usr/local/bin / directory using the following commands

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

If the above installation process is very slow, you can try to install it through Composer domestic mirror 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 Framework

Install the latest version of the ThinkPHP5 framework into the thinkSwooleFirst folder in the current directory using the following commands

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

Install the think-swoole Composer package

Switch the command line directory to the thinkSwooleFirst directory and install think-swoole through Composer

$ > composer require topthink/think-swoole

Swoole PECL extensions must be installed before installing the think-swoole Composer package

HelloSwoole

When the environment is fully built, we create Demon.php files in the application/index/controller directory to write Hello World for Swoole and ThinkPHP5 TCP servers

<?php
namespace app\index\controller;

// Must use and inherit the think swoole Server class
use think\swoole\Server;

class Demon extends Server
{
    // Monitor all addresses
    protected $host = '0.0.0.0';
    // Listen on port 9501
    protected $port = 9501;
    // Specify run mode as multi-process
    protected $mode = SWOOLE_PROCESS;
    // Specify tcp socket of type ipv4
    protected $sockType = SWOOLE_SOCK_TCP;
    // Configuration Items
    protected $option = [
        /** 
         *  Set the number of worker processes started
         *  Business code is fully asynchronous and non-blocking. It is most reasonable to set it to 1-4 times CPU.
         *  Business code is synchronous blocking and needs to be adjusted according to request response time and system load.
         */
        'worker_num' => 4,
        // Daemonization
        'daemonize'  => true,
        // Monitor queue length
        'backlog'    => 128
    ];

    /**
     * Callback function for receiving information
     * @param \swoole_server $serv swoole_server object
     * @param $fd TCP File Descriptor for Client Connection
     * @param $from_id TCP Reactor thread ID where the connection is located
     * @param $data Data content received
     */
    public function onReceive(\swoole_server $server, $fd, $from_id, $data)
    {
        $server->send($fd, 'onReceive: ' . $data);
    }
}

Even if the above code uses ThinkPHP5 and Swoole to complete a relatively simple TCP server, then you can run the server.
Start the TCP server with the following commands

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

We can also make it easier for us to debug by modifying the configuration item deamonitor to false so that the program does not use daemons.

Using daemon mode, we can also use lsof tool to detect whether the port is in normal state.

$ > lsof -i:9501

When the confirmation program is running properly, use the telnet tool 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

When you enter hello, you get the following results

onReceive: hello

For more complete examples of Swoole and ThinkPHP5, visit my new book When Swoole Meets ThinkPHP5

Posted by flaab on Tue, 16 Jul 2019 10:51:08 -0700