Home > PHP Framework > ThinkPHP > body text

TP5 integrates WorkerMan and GatewayWorker

藏色散人
Release: 2019-12-30 13:54:02
forward
3139 people have browsed it

TP5 integrates GatewayWorker

Windows version installation

a) Use composer create-project topthink/think testTG to install thinkphp5.

b) Enter the directory of thinkphp5, here is testTG, and use composer require workerman/gateway-worker-for-win to install the Windows version of gateway.

c) Go to the official website to download the Windows version of gateway-worker, which contains a demo. http://www.workerman.net/download

d) Unzip the downloaded compressed package and copy all the files in Applications/Yourapp to any folder in the thinkphp5 directory application, here named push .

e) Copy start_for_win.bat in the decompressed folder to the root directory of thinkphp5, which is the directory at the same level as application.

f) Right-click start_for_win.bat, click Edit, change the directory inside to your own directory, here it is changed to

php application\push\start_register.php application\push\start_gateway.php application\push\start_businessworker.php
Pause
Copy after login

g) Save and exit. Double click to run.

Linux version installation

a) Use composer create-project topthink/think testTG to install thinkphp5.

b) Enter the directory of thinkphp5, Here is testTG. Use composer require workerman/gateway-worker to install the Linux version of gateway.

c) Go to the official website to download the Linux version of gateway-worker, which contains a demo. http://www.workerman.net/download

d) Unzip the downloaded compressed package and copy all the files in Applications/Yourapp to any folder in the thinkphp5 directory application, here named push .

e) Copy start.php in the decompressed folder to the root directory of thinkphp5, which is a directory at the same level as application.

f) Change the path in the forearch loop brackets in the last part of the start.php file to your correct path.

Start on the command line php start.php start.

TP5 integrationWrokerMan

Windows version installation

a) Use composer create-project topthink /think testTW to install thinkphp5.

b) Enter the thinkphp5 root directory, which is testTW. First use composer require topthink/think-worker,

and then use composer require workerman/workerman-for-win to install workererman. After successful installation, delete vendor\workerman\workerman.

c) Create server.php in the thinkphp5 root directory (that is, the directory at the same level as application) and edit the content.

Copy after login

d) Create workerman’s controller and name it Worker.php. In application/push/controller, the directory does not exist and is created by itself. Add the following content:

protected $socket = 'websocket://127.0.0.1:2346' where 127.0.0.1 is the IP address of the socket server. Listen here to port 2346 of this machine.

send('我收到你的信息了');
    }
 
    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection)
    {
 
    }
 
    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection)
    {
        
    }
    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg)
    {
        echo "error $code $msg\n";
    }
 
    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker)
    {
 
    }
}
Copy after login

e) Run from the command line and start the listening service php server.php

f) Create a new html file at any location. The content is:


Copy after login

g) Save it and open it with a browser, you can see that the link is successful.

Linux version installation

a) Just execute the composer command in step b) of Windows version installation: composer require topthink/think-worker. That’s it, the rest of the steps remain unchanged.

Simple use of GatewayWorker


class Push{
   
    public function helloAction () {
        $uid = $_GET['uid'];
        session('uid', $uid);
 
        $view = new View;
        return $view->fetch();
    }
 
    public function BindClientIdAction () {
        
        $client_id = $_POST['client_id'];
        // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值
        Gateway::$registerAddress = '127.0.0.1:1238';
 
        $bindUid = session('uid');
        // 假设用户已经登录,用户uid和群组id在session中
        // client_id与uid绑定
        Gateway::bindUid($client_id, $bindUid);
        // 加入某个群组(可调用多次加入多个群组)
        // Gateway::joinGroup($client_id, $group_id);
    }
 
    public function AjaxSendMessageAction () {
        $message = $_POST['message'];
        // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值
        Gateway::$registerAddress = '127.0.0.1:1238';
 
        GateWay::sendToAll($message);
    }
}
Copy after login

The above is the detailed content of TP5 integrates WorkerMan and GatewayWorker. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
tp5
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!