When Swoole meets ThinkPHP5 world Hello

Source: Internet
Author: User
Tags readfile

This article assumes that you already have a Linux operating system PHP environment, it is highly recommended to use Vagrant to build the development environment
Installation Swoole PECL expansion

Can be installed through the PECL command or through the source package, this article uses the PECL command to install swoole on the command line input

$ > pecl install swoole

Install swoole PECL extension, after executing the command through the PHP-M command to check whether the installation of Swoole is successful, if the returned extension list contains swoole indicates that the installation is successful, you can also check phpinfo to see if the installation was successful.
Installing the THINKPHP5 Framework
1. Installing Composer

If you have already installed Composer, you can skip this step, but be sure to use the Composer self-update command to ensure that you have used the latest version of Composer using the following command to download the Composer directly via the Composer.phar website and automatically installed 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 mirror 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. Installing the ThinkPHP5 Frame

Use the following command to install the latest version of the THINKPHP5 framework into the Thinkswoolefirst folder in the current directory

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

Installing the Think-swoole Composer package

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

$ > composer require topthink/think-swoole

The Swoole PECL must be installed before installing the Think-swoole Composer package.

Helloswoole

When the environment is complete, we build the demon.php file in the Application/index/controller directory to write the Hello world of the TCP server Swoole and ThinkPHP5

<?phpnamespace app\index\controller;//must use and inherit \think\swoole\server class use Think\swoole\server;class Demon extends  server{   //monitor all addresses     protected $host = ' 0.0.0.0 ';   //monitor 9501 ports     protected $port = 9501;   //Specify run mode for multi-process     protected $mode = swoole_process;   //Specify socket Type IP V4 TCP socket    Protected $sockType = swoole_sock_tcp;   //configuration Items     protected $option = [        /**          *   Set the number of worker processes started         & nbsp;*   Business code is fully asynchronous non-blocking, which is set to 1-4 times the most reasonable of the CPU          *   business code for synchronous blocking, which needs to be adjusted according to request response time and system load          */        ' worker_num ' + 4,       //Daemon Process & nbsp       ' daemonize '  => true,       //monitor Queue Length         ' Backl OG '    => 128   ];   /**     * callback function when receiving information      * @param \swoole_server $serv Swoole_server Object      * @param $fd TCP Client Connection file descriptor      * @param $from _id TCP connection Reactor thread id    &NBS p;* @param $data received data content      */    Public Function onreceive (\swoole_server $server, $FD, $from _i D, $data)     {        $server->send ($FD, ' onreceive: '. $data);   }}

The above code uses THINKPHP5 and Swoole to complete a relatively simple TCP server, then you can run the server using the following command to start the TCP server

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

We can also modify the configuration item deamonize to False, so that the program does not use daemons, more convenient for our debugging

Using daemon mode We can also use the Lsof tool to detect if the port is in a healthy state

$ > lsof -i:9501

After confirming that the program is working properly, use the Telnet tool to connect to the TCP server, and later in the article we will also talk about if the TCP client using Swoole to connect to the TCP server

Telnet 127.0.0.1 9501

When you enter Hello, you get the following results

Onreceive:hello

When Swoole meets ThinkPHP5 world Hello

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.