亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home PHP Framework Workerman How to use workererman in Thinkphp5.1

How to use workererman in Thinkphp5.1

Feb 10, 2020 pm 04:58 PM
thinkphp workerman

Workerman is an open source, high-performance asynchronous PHP socket framework developed purely in PHP. ThinkPHP is a fast, compatible and simple lightweight domestic PHP development framework. This article will introduce to you how to use workererman in Thinkphp5.1.

How to use workererman in Thinkphp5.1

I have been using swoole before, and recently I studied workerman, so I installed composer

composer require workerman/workerman

Write a test code in the Thinkphp controller

<?php
namespace app\workerman\controller;
use think\Controller;
use Workerman\Worker;

class Index extends Controller
{

    public function index()
    {
        // 創(chuàng)建一個Worker監(jiān)聽2345端口,使用http協(xié)議通訊
        $http_worker = new Worker("http://0.0.0.0:2345");

        // 啟動4個進程對外提供服務
        $http_worker->count = 4;

        // 接收到瀏覽器發(fā)送的數(shù)據(jù)時回復hello world給瀏覽器
        $http_worker->onMessage = function($connection, $data)
        {
            // 向瀏覽器發(fā)送hello world
            $connection->send(&#39;hello world&#39;);
        };
        // 運行worker
        Worker::runAll();
    }

}

Command line execution: php index.php workerman/index. I thought it was done, but the following prompt was reported:

How to use workererman in Thinkphp5.1

Obviously, Workerman cannot directly run the file. According to the official document, use

php index .php start
php index.php stop
php index.php restart

is executed in this format. So I modified the index.php file to bind the route

// [ 應用入口文件 ]
namespace think;

// 加載基礎文件
require __DIR__ . &#39;/../thinkphp/base.php&#39;;

// 支持事先使用靜態(tài)方法設置Request對象和Config對象

// 執(zhí)行應用并響應
Container::get(&#39;app&#39;)->bind("workerman/index")->run()->send();

and ran php index.php start directly. Unfortunately, it was prompted that the start model could not be found. Why is tp5 parsing start as a route. What should I do? Workerman needs to be executed using the start method, but tp5 needs to parse the parameters into a model.

After checking the information, I found that Thinkphp5.1 itself integrated workererman. You can use thinkphp5 to install workererman, then you can run it using thinkphp's running mode.

The execution command is changed to:

php think worker

Later, I found that the workerman package integrated by Thinkphp5.1 is a bit troublesome and difficult to use, and if you want to use the workerman service such as PHPSocketIO, use the integrated method Very troublesome.

workerman uses the first parameter as the command to operate the service. Can I change it to use the second parameter as the command to operate the service?

That’s exactly what happened. Find the parseCommand() function in the workerman plug-in. This ghost function is to get the operation command, change:

argv[1] to argv[2], argv[2] to argv[2], change argv[2] to argv[3 ]

    protected static function parseCommand()
    {
        if (static::$_OS !== OS_TYPE_LINUX) {
            return;
        }
        global $argv;
        // Check argv;
        $start_file = $argv[0];
        $available_commands = array(
            &#39;start&#39;,
            &#39;stop&#39;,
            &#39;restart&#39;,
            &#39;reload&#39;,
            &#39;status&#39;,
            &#39;connections&#39;,
        );
        $usage = "Usage: php yourfile <command> 
        [mode]\nCommands: \nstart\t\tStart worker in DEBUG mode.\n\t\tUse mode -d to start in DAEMON mode.\nstop\t\tStop worker.\n\t\tUse mode -g to stop gracefully.\nrestart\t\tRestart workers.\n\t\tUse mode -d to start in DAEMON mode.\n\t\tUse mode -g to stop gracefully.\nreload\t\tReload codes.\n\t\tUse mode -g to reload gracefully.\nstatus\t\tGet worker status.\n\t\tUse mode -d to show live status.\nconnections\tGet worker connections.\n";
        if (!isset($argv[2]) || !in_array($argv[2], $available_commands)) {
            if (isset($argv[2])) {
                static::safeEcho(&#39;Unknown command: &#39; . $argv[2] . "\n");
            }
            exit($usage);
        }

        // Get command.
        $command  = trim($argv[2]);
        $command2 = isset($argv[3]) ? $argv[3] : &#39;&#39;;

The execution command is changed to

php server.php index start

(the first parameter is used for Thinkphp to parse the routing, and the second parameter is used for the workerman to parse the operation service command)

For more workerman knowledge, please pay attention to the tutorial column of the PHP Chinese website workerman Framework.

The above is the detailed content of How to use workererman in Thinkphp5.1. 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Hot Topics

PHP Tutorial
1488
72
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

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.

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.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

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.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

See all articles