Home PHP Framework Workerman How to use workerman with thinkphp

How to use workerman with thinkphp

Nov 30, 2019 pm 04:53 PM
thinkphp workerman

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

Implement file upload and download in Workerman documents Implement file upload and download in Workerman documents Nov 08, 2023 pm 06:02 PM

To implement file upload and download in Workerman documents, specific code examples are required. Introduction: Workerman is a high-performance PHP asynchronous network communication framework that is simple, efficient, and easy to use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples. 1. File upload: File upload refers to the operation of transferring files on the local computer to the server. The following is used

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

How to implement the basic usage of Workerman documents How to implement the basic usage of Workerman documents Nov 08, 2023 am 11:46 AM

Introduction to how to implement the basic usage of Workerman documents: Workerman is a high-performance PHP development framework that can help developers easily build high-concurrency network applications. This article will introduce the basic usage of Workerman, including installation and configuration, creating services and listening ports, handling client requests, etc. And give corresponding code examples. 1. Install and configure Workerman. Enter the following command on the command line to install Workerman: c

Which one is better, swoole or workerman? Which one is better, swoole or workerman? Apr 09, 2024 pm 07:00 PM

Swoole and Workerman are both high-performance PHP server frameworks. Known for its asynchronous processing, excellent performance, and scalability, Swoole is suitable for projects that need to handle a large number of concurrent requests and high throughput. Workerman offers the flexibility of both asynchronous and synchronous modes, with an intuitive API that is better suited for ease of use and projects that handle lower concurrency volumes.

See all articles