Laravel开发实践laravel如何优雅的用laravel核心

Laravel 中使用 Workerman插件 进行 socke

2019-07-26  本文已影响4人  山与清川

安装方式

步骤一、安装插件

composer require workerman/gateway-worker

步骤二、安装完毕后在‘app\Console\Commands’创建文件'WorkermanCommand.php'

<?php

namespace App\Console\Commands;

use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use Illuminate\Console\Command;
use Workerman\Worker;

class WorkermanCommand extends Command
{

    protected $signature = 'workman {action} {--d}';

    protected $description = 'Start a Workerman server.';

    public function handle()
    {
        global $argv;
        $action = $this->argument('action');

        $argv[0] = 'wk';
        $argv[1] = $action;
        $argv[2] = $this->option('d') ? '-d' : '';

        $this->start();
    }

    private function start()
    {
        $this->startGateWay();
        $this->startBusinessWorker();
        $this->startRegister();
        Worker::runAll();
    }

    private function startBusinessWorker()
    {
        $worker                  = new BusinessWorker();
        $worker->name            = 'BusinessWorker';
        $worker->count           = 1;
        $worker->registerAddress = '127.0.0.1:1236';
        $worker->eventHandler    = \App\Workerman\Events::class;
    }

    private function startGateWay()
    {
        $gateway = new Gateway("websocket://0.0.0.0:8890");
        $gateway->name                 = 'Gateway';
        $gateway->count                = 1;
        $gateway->lanIp                = '127.0.0.1';
        $gateway->startPort            = 2300;
        $gateway->pingInterval         = 30;
        $gateway->pingNotResponseLimit = 0;
        $gateway->pingData             = '{"type":"@heart@"}';
        $gateway->registerAddress      = '127.0.0.1:1236';
    }

    private function startRegister()
    {
        new Register('text://0.0.0.0:1236');
    }
}

步骤三、创建‘app\Workerman\Events.php’文件

<?php

namespace App\Workerman;

class Events
{

    public static function onWorkerStart($businessWorker)
    {
    }

    public static function onConnect($client_id)
    {
    }

    public static function onWebSocketConnect($client_id, $data)
    {
    }

    public static function onMessage($client_id, $message)
    {
    }

    public static function onClose($client_id)
    {
    }
}

应用教程:

一、启动命令方式

在命令行里面执行,支持的命令大概有 start|stop|restart|status,其中 -d 或--d 的意思是 daemon 模式。

php artisan workman start -d 部分环境用这个 后台执行
php artisan workman start --d  部分环境是这个  后台执行
php artisan workman start   单次执行
php artisan workman stop  结束
php artisan workman restart 重启
php artisan workman status 查看状态

注:在项目根目录中输入命令,其他地方无效。

二、php作为客户端连接其他workenman

// 设置访问对方主机的本地ip及端口(每个socket连接都会占用一个本地端口)
        $context_option = array(
            'socket' => array(
                // ip必须是本机网卡ip,并且能访问对方主机,否则无效
                'bindto' =>'ip:端口',
            ),
        );

        $con = new AsyncTcpConnection('ws://ip:端口', $context_option);

        $con->onConnect = function($con) {

            $result = '';

            if($result){
                $result = json_decode($result['JsonResult']);
                $con->send('消息内容');
                dump('建立了连接:'.json_encode($result));
            }

        };

        $con->onMessage = function($con, $data) {
            dump('接收了数据:'.json_encode($data));
            sleep(2);
            dump('发送了消息:');
            $con->send($result);
        };

        $con->onClose = function($con) {
            // 如果连接断开,则在1秒后重连
            $con->reConnect(1);
        };

        $con->connect();

注:加入在worker的开始'onWorkerStart' 方法中。

上一篇 下一篇

猜你喜欢

热点阅读