Home > PHP Framework > Workerman > body text

How to use workerman with thinkphp

Release: 2019-11-30 16:53:09
forward
5474 people have browsed it

The following column workerman Getting Started Tutorial will introduce to you the method of combining workerman with thinkphp. I hope it will be of some help to you.

How to use workerman with thinkphp

To run workererman you need to install pcntl and event or libevent

pcntl installation method:

With php-5.5 .20 as an example, the actual situation is based on the PHP directory you installed

1. Find the PHP source code, enter the php-5.5.20/ext/pcntl/ directory cd php-5.5.20/ext/pcntl/

2. Run locate phpize to find the phpize directory and run /usr/local/php-5.5.20/bin/phpize

3. Execute ./configure --with-php-config=PHP Configuration file path For example: ./configure --with-php-config=/usr/local/php-5.5.20/bin/php-config

4. Compile and install make && make install If/ext /pcntl/modules/pcntl.so is generated and compiled successfully

5. Add extension to PHP.INI loaded by php echo "extension=pcntl.so" >> /etc/php.ini ( Enter the command php --ini to view the currently used php.ini configuration path)

6. Restart nginx nginx -s reload

7. View the service ps -aux | grep pcntl

If it is version 5.3, you can directly enter the command to install:

yum install php-cli php-process git gcc php-devel php-pear libevent-devel php-pdo php-mysql_pdo -y
Copy after login

event installation method:

1. yum install libevent-devel -y

2. pecl install event

Tips: Include libevent OpenSSL support [yes]: Enter no and press Enter, otherwise just press Enter

3. echo extension=event.so > /etc/php.ini

libevent installation method:

1. yum install libevent-devel

2. pecl install channel://pecl .php.net/libevent-0.1.0 //Prompt libevent installation [autodetect]: Press Enter

3. Check the PHP directory lib/php/extensions/no-debug-non-zts-20121212 Whether libevent.so is generated under the directory

4. Enter the command php -v to view the installed extensions

Workerman is integrated into Thinkphp: (recommended: workerman Tutorial)

1. Place the downloaded workerman directory into the project\ThinkPHP\Library directory

2. Place the class files in the workerman directory except \Lib\Constants.php Change the file name to *.class.php

3. Create a new file worker.php file in the project root directory with the following content:

<?php
header("Content-type: text/html; charset=utf-8");
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用入口文件
define(&#39;BIND_MODULE&#39;, &#39;Worker&#39;);
define(&#39;BIND_CONTROLLER&#39;, &#39;Worker&#39;);
define(&#39;BIND_ACTION&#39;, &#39;Start&#39;);
//define(&#39;APP_MODE&#39;,&#39;cli&#39;);
//ThinkPHP的其他设定
define( &#39;APP_PATH&#39;, dirname(__FILE__).&#39;/Application/&#39; );
require dirname( __FILE__).&#39;/ThinkPHP/ThinkPHP.php&#39;;
?>
Copy after login

4. Copy a copy of the Home module and change its name to Worker

5. Create a new Worker controller in the Worker module with the following content:

<?php
namespace Worker\Controller;
use Think\Controller;
use Workerman\Worker;
class WorkerController extends Controller{
    public function Start() {
        $worker = new Worker(&#39;text://0.0.0.0:8989&#39;); //实例化Worker,并设置协议、IP和端口(地址和端口自定义)
        $worker->count = 4;
        $worker->onMessage = array($this, &#39;onMessage&#39;);
        //worker的其它回调方法可以参考onMessage
        Worker::runAll();
    }
    public function onMessage($connection, $data)
    {
        $connection->send(&#39;hello&#39;);
    }
    
}
 ?>
Copy after login

6. Enter the Linux system and open port 8989

iptables -I INPUT -p tcp --dport 8989 -j ACCEPT
Copy after login

7. Enter the project root Enter the startup command in the directory:

php worker.php start
Copy after login

displays OK, indicating that the startup is successful

8. Create a new server link and enter telnet 127.0.0.1 8989 to test

Workerman accesses MySQL :

1. Install pdo and pdo_mysql and other related extensions

2. Add database-related configurations to config.php in the Conf directory of the Worker module

3. Do not use localhost for the server address, use IP127.0.0.1

4. After configuring the database, you can directly use Thinkphp’s Db method



The above is the detailed content of How to use workerman with thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template