Workerman启动有端口号可以连接,GatewayWorker启动没端口号连接不了

苏三宝

![](//www.workerman.net/upload/20201112/125fac9b9a7d79.png)
workerman可以连接的上,到是用GatewayWorker用前端链接的时候不行而且启动没端口号

1623 3 0
3个回答

苏三宝

  • 暂无评论
q13113671764

估计报错了,不知道你怎么写的,用tp5.1有集成Gateway

  • 苏三宝 2020-11-12

    就是按照tp5.1教程搞的,启动没有端口很郁闷

苏三宝
  • q13113671764 2020-11-12

    5.1 要那个教程干啥,tp官方有集成,
    这是 config 里面的 gateway_worker.php
    'businessWorker' => [
    'name' => 'BusinessWorker',
    'count' => 1,
    //重写Events类
    'eventHandler' => 'app\worker\controller\GateWayMsg',
    ],

    然后 GateWayMsg 代码

    <?php

    namespace app\worker\controller;

    use GatewayWorker\Lib\Gateway;
    use think\worker\Events;
    use Workerman\Lib\Timer;
    use Workerman\Worker;

    class GateWayMsg extends Events
    {

    /**
     * onMessage 事件回调
     * 当客户端发来数据(Gateway进程收到数据)后触发
     *
     * @access public
     * @param int $client_id
     * @param mixed $data
     * @return void
     */
    public static function onMessage($client_id, $data)
    {
    
        $data = json_decode($data, true);
    
    }
    
    //
    
    /**
     * onClose 事件回调 当用户断开连接时触发的方法
     *
     * @param integer $client_id 断开连接的客户端client_id
     * @return void
     */
    public static function onClose($client_id)
    {
        GateWay::sendToAll("client[$client_id] logout\n");
    }
    
    /**
     * onConnect 事件回调
     * 当客户端连接上gateway进程时(TCP三次握手完毕时)触发
     *
     * @access public
     * @param int $client_id
     * @return void
     */
    public static function onConnect($client_id)
    {
    
        // 连接到来后,定时10秒关闭这个链接,需要10秒内发认证并删除定时器阻止关闭连接的执行
        $auth_timer_id = Timer::add(10, function ($client_id) {
            Gateway::closeClient($client_id);
        }, array($client_id), false);
    
      Gateway::updateSession($client_id, array('auth_timer_id' => $auth_timer_id));
    }
    
    public static function onWorkerStart(Worker $businessWorker)
    {
        parent::onWorkerStart($businessWorker); // TODO: Change the autogenerated stub
    
    }
    
    /**
     * onWorkerStop 事件回调
     * 当businessWorker进程退出时触发。每个进程生命周期内都只会触发一次。
     *
     * @param \Workerman\Worker $businessWorker
     * @return void
     */
    public static function onWorkerStop(Worker $businessWorker)
    {
        echo "WorkerStop\n";
    }

    }

  • q13113671764 2020-11-12

    最后 php think worker:gateway 启动,这个要在linux运行

  • 苏三宝 2020-11-13

    @5433:谢谢指导

年代过于久远,无法发表回答
🔝