walkor / GatewayWorker

Distributed realtime messaging framework based on workerman.
MIT License
1.01k stars 294 forks source link

Do not want to use static functions in Events.php file #81

Closed abhinavgmfs closed 2 years ago

abhinavgmfs commented 2 years ago

Hi Walkor,

Previously we were using workerman so we have used onMessage, OnWorkerStart etc without static method and we have used $this in the whole project that are used for workerman.

Now we want to use the same code in the GatewayWorker but we are facing issue when we using $this keyword inside static functions. We want to keep the older code same as current because there are lots of functions and variables in our existing project are using $this keyword.

Is there any way we can use these function without static keyword?

For Examples:-

public static function onWorkerStart($businessWorker)
{
   $this->example= new example();
   $this->example->socketStartAlert(SYS_ID_ALERT);
}
walkor commented 2 years ago

A better way is create your own BusinessWorker which extends GatewayWorker\BusinessWorker. The codes may like this.

class MyBusinessWorker extends GatewayWorker\BusinessWorker {
    protected function onWorkerStart()
    {
        static $event_instance = null;
        $event_instance = new $this->eventHandler;
        if (function_exists('opcache_reset')) {
            opcache_reset();
        }

        if (!class_exists('\Protocols\GatewayProtocol')) {
            class_alias('GatewayWorker\Protocols\GatewayProtocol', 'Protocols\GatewayProtocol');
        }

        if (!is_array($this->registerAddress)) {
            $this->registerAddress = array($this->registerAddress);
        }
        $this->connectToRegister();

        \GatewayWorker\Lib\Gateway::setBusinessWorker($this);
        \GatewayWorker\Lib\Gateway::$secretKey = $this->secretKey;
        if ($this->_onWorkerStart) {
            call_user_func($this->_onWorkerStart, $this);
        }

        if (is_callable([$event_instance, 'onWorkerStart'])) {
            call_user_func([$event_instance, 'onWorkerStart'], $this);
        }

        if (function_exists('pcntl_signal')) {
            pcntl_signal(SIGALRM, array($this, 'timeoutHandler'), false);
        } else {
            $this->processTimeout = 0;
        }
        if (is_callable([$event_instance, 'onConnect'])) {
            $this->_eventOnConnect = [$event_instance, 'onConnect'];
        }

        if (is_callable([$event_instance, 'onMessage'])) {
            $this->_eventOnMessage = [$event_instance, 'onMessage'];
        } else {
            echo "Waring: {$this->eventHandler}::onMessage is not callable\n";
        }

        if (is_callable([$event_instance, 'onClose'])) {
            $this->_eventOnClose = [$event_instance, 'onClose']
        }

        if (is_callable([$event_instance, 'onWebSocketConnect'])) {
            $this->_eventOnWebSocketConnect = [$event_instance, 'onWebSocketConnect'];
        }

    }
}
abhinavgmfs commented 2 years ago

Thank you!