在 Laravel 中使用 Workerman 進行 socket 通訊

小陳叔叔發表於2018-06-12

由於要使用客戶端點對點通訊,選擇了 workerman/gateway-worker 的擴充套件包,它已經引入了 workerman/workerman

$ composer require workerman/gateway-worker

建立一個 artisan 命令列工具來啟動 Socket 服務端,在 app/Console/Commands 目錄下建立命令列檔案。

<?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:2346");
        $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 檔案來監聽處理 workman 的各種事件。

<?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,其中 -d 的意思是 daemon 模式。

$ php artisan workman start -d

當你看到如下結果的時候,workman已經啟動成功了。

Workerman[wk] start in DEBUG mode
----------------------- WORKERMAN -----------------------------
Workerman version:3.5.11          PHP version:7.1.11
------------------------ WORKERS -------------------------------
user          worker          listen                    processes status
root          Gateway         websocket://0.0.0.0:2346   1         [OK]
root          BusinessWorker  none                       1         [OK]
root          Register        text://0.0.0.0:1236        1         [OK]
----------------------------------------------------------------
Press Ctrl+C to stop. Start success.
onWorkerStart

表述能力不是很好,按照這個流程,可以很快的搭建起基於 workerman 的 socket 服務端,其中的執行配置引數等,大家自行百度吧。

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章