PHP when Swoole meets ThinkPHP5

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

Install Swoole PECL expansion
It can be compiled and installed through pecl command or source package. In this paper, we use pecl command to install Swoole and input it on the command line

$ > pecl install swoole

 

Install the Swoole PECL extension. After executing the command, use the php-m command to check whether the installation of swoole is successful. If the expansion list returned contains swoole, it indicates that the installation is successful. You can also check whether the installation of swoole is successful through phpinfo.

Install the ThinkPHP5 framework 1. Install Composer if you have already installed Composer, you can skip this step, but make sure to use the Composer self update command to ensure that you have used the latest version of Composer. Use the following command to download Composer.phar directly from the Composer official website and install it to / usr/local/bin / directory automatically

$ > 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 through Composer domestic image in the following ways.

$ > 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 the ThinkPHP5 framework into the thinksoolefirst folder in the current directory

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

 

Install the think swoole composer package

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

$ > composer require topthink/think-swoole

Before installing the think swoole composer package, you must install the Swoole PECL extension

HelloSwoole

When the environment is all set up, we create the demo.php file in the application/index/controller directory to write the Hello World of the TCP server of Swoole and ThinkPHP5

 1 <?php
 2 namespace app\index\controller;
 3 
 4 // Must use And inherit \think\swoole\Server class
 5 use think\swoole\Server;
 6 
 7 class Demon extends Server
 8 {
 9     // Listen to all addresses
10     protected $host = '0.0.0.0';
11     // Listen to port 9501
12     protected $port = 9501;
13     // Specify the running mode as multi process
14     protected $mode = SWOOLE_PROCESS;
15     // Appoint socket The type is ipv4 Of tcp socket
16     protected $sockType = SWOOLE_SOCK_TCP;
17     // Configuration item
18     protected $option = [
19         /** 
20          *  Set the number of worker processes started
21          *  The business code is full asynchronous and non blocking. It is most reasonable to set it as 1-4 times of the CPU
22          *  The business code is synchronous blocking, which needs to be adjusted according to the request response time and system load
23          */
24         'worker_num' => 4,
25         // Daemonization
26         'daemonize'  => true,
27         // Length of listening queue
28         'backlog'    => 128
29     ];
30 
31     /**
32      * Callback function when receiving information
33      * @param \swoole_server $serv swoole_server object
34      * @param $fd TCP File descriptor for client connection
35      * @param $from_id TCP The Reactor thread ID of the connection
36      * @param $data Data content received
37      */
38     public function onReceive(\swoole_server $server, $fd, $from_id, $data)
39     {
40         $server->send($fd, 'onReceive: ' . $data);
41     }
42 }

The above code uses ThinkPHP5 and Swoole to complete a relatively simple TCP server. Next, you can run the server to start the TCP server with the following command

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

 

We can also change the configuration item de amomize to false, so that the program does not use daemons, which is more convenient for our debugging

Using daemons, we can also use lsof to check whether the port is in normal state

We can also change the configuration item de amomize to false, so that the program does not use daemons, which is more convenient for our debugging

Using daemons, we can also use lsof to check whether the port is in normal state

$ > lsof -i:9501

 

After confirming the normal operation of the program, use the telnet tool to connect to the TCP server. We will also talk about how to use the TCP client of swoole to connect to the TCP server in a later article

telnet 127.0.0.1 9501

 

Enter hello and you will get the following results

onReceive: hello

 

Tags: PHP lsof socket Linux

Posted on Tue, 05 Nov 2019 01:49:24 -0800 by beckjoh