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

How to use workererman in Thinkphp5.1

Release: 2020-02-10 16:58:20
forward
11796 people have browsed it

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
Copy after login

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()
    {
        // 创建一个Worker监听2345端口,使用http协议通讯
        $http_worker = new Worker("http://0.0.0.0:2345");

        // 启动4个进程对外提供服务
        $http_worker->count = 4;

        // 接收到浏览器发送的数据时回复hello world给浏览器
        $http_worker->onMessage = function($connection, $data)
        {
            // 向浏览器发送hello world
            $connection->send(&#39;hello world&#39;);
        };
        // 运行worker
        Worker::runAll();
    }

}
Copy after login

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;;

// 支持事先使用静态方法设置Request对象和Config对象

// 执行应用并响应
Container::get(&#39;app&#39;)->bind("workerman/index")->run()->send();
Copy after login

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
Copy after login

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;;
Copy after login

The execution command is changed to

php server.php index start
Copy after login

(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!

Related labels:
source:csdn.net
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