当前位置: 首页 > 工具软件 > Workerman > 使用案例 >

laravel+workerman(ws)

裴焱
2023-12-01
<?php

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

class WorkermanWebs extends Command
{
    protected $signature = 'wk:webs  {action} {--d}';
    protected $description = 'workerman websockets';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        global $argv;
        $action = $this->argument('action');
        $argv[0] = 'wk:webs';
        $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:8088");
        $gateway->name                 = 'Gateway';
        $gateway->count                = 1;
        $gateway->lanIp                = '0.0.0.0';
        $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');
    }
}
<?php


namespace App\Workerman;


use GatewayWorker\Lib\Gateway;
use Illuminate\Support\Facades\Log;

class Events
{
    public static function onWorkerStart($businessWorker)
    {
        echo "onWorkerStart\r\n";
    }

    public static function onConnect($client_id)
    {
        Gateway::sendToClient($client_id, json_encode(['type' => 'onConnect', 'client_id' => $client_id]));
        echo "onConnect\r\n";

    }

    public static function onWebSocketConnect($client_id, $data)
    {
        echo "onWebSocketConnect\r\n";
    }

    public static function onMessage($client_id, $message)
    {
        $data = json_decode($message, true);
        if (isset($data['imei'])) Gateway::bindUid($client_id, $data['imei']);
        Gateway::sendToClient($client_id, json_encode(['type' => 'onMessage', 'client_id' => $client_id]));
        echo "onMessage\r\n";

    }

    public static function onClose($client_id)
    {
        echo "onClose\r\n";
    }

}
<?php


namespace App\Http\Controllers;


use App\Events\SendUid;
use App\Workerman\Events;
use GatewayClient\Gateway;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;


class TestController extends Controller
{
    public function index(Request $request)
    {
        $uid = $request->get('uid');
        try {
            Gateway::$registerAddress = 'xx.xx.xx.xx:1236';
//            $arr = Gateway::getClientIdByUid($uid);
//            Log::info($arr);
//            Log::info('$arr');
//            if(count($arr) >0)
            Gateway::sendToUid($uid, json_encode(['type' => 'onHhx', 'message' => 'success']));
            return true;
        } catch (\Exception $exception) {
            Log::info($exception->getMessage());
        }
    }
}
stream_socket_client(): unable to connect to tcp://0.0.0.0:xxxx (Connection refused)

修改GatewayClient文件

在stream_socket_client()之前将地址改过来

文件vendor/workerman/gatewayclient/Gateway.php的1186行下添加如下代码

$address = str_replace('0.0.0.0', explode(':', self::$registerAddress)[0], $address);
 类似资料: