walkor / phpsocket.io

A server side alternative implementation of socket.io in PHP based on workerman.
2.3k stars 508 forks source link

Problem to share variable between process #267

Closed marcosmarcolin closed 3 years ago

marcosmarcolin commented 3 years ago

Hi guys, I hope you're alright.

I have the following situation and problem.

I'm starting two workers, one for the socket and the other to run every so many seconds, example:

$io = new SocketIO($port, $cert);
$io->on('connect', function (){
    // code
});

$task = new Worker();
    $task->onWorkerStart = function ($task) {
        Timer::add(5, function () {

        });
    };

Worker::runAll();

In this case, generate two processes (pids), I needed to use the variable $io inside the timer, but it is not possible.

Tried to use memory with Memcached and workerman/globaldata but unable to serialize a SocketIO() object because of anonymous functions, does anyone have any other ideas what to do?

`Exception: Serialization of 'Closure' is not allowed in /var/www/socket-io.php:57 Stack trace:

0 /var/www/socket-io.php(57): serialize(Object(PHPSocketIO\SocketIO))

1 [internal function]: {closure}(Object(PHPSocketIO\Socket))

2 /var/www/vendor/workerman/phpsocket.io/src/Event/Emitter.php(93): call_user_func_array(Object(Closure), Array)

3 [internal function]: PHPSocketIO\Event\Emitter->emit('connection', Object(PHPSocketIO\Socket))

4 /var/www/vendor/workerman/phpsocket.io/src/Nsp.php(102): call_user_func_array(Array, Array)

5 /var/www/vendor/workerman/phpsocket.io/src/Nsp.php(68): PHPSocketIO\Nsp->emit('connection', Object(PHPSocketIO\Socket))

6 /var/www/vendor/workerman/phpsocket.io/src/Client.php(64): PHPSocketIO\Nsp->add(Object(PHPSocketIO\Client), Object(PHPSocketIO\Nsp), Array)

7 /var/www/vendor/workerman/phpsocket.io/src/SocketIO.php(130): PHPSocketIO\Client->connect('/')

8 [internal function]: PHPSocketIO\SocketIO->onConnection(Object(PHPSocketIO\Engine\Socket))

9 /var/www/vendor/workerman/phpsocket.io/src/Event/Emitter.php(93): call_user_func_array(Array, Array)

`

Even using use() in Timer, it takes only the variable with the created value, it doesn't take its subsequent change, even passing by reference.

Was the problem clear?

walkor commented 3 years ago

I think add timer in socketio process is a better way which codes like this.

$io = new SocketIO($port, $cert);
$io->on('connect', function (){
    // code
});
$io->on('workerStart', function()use($io) {
    Timer::add(5, function () use ($io){
        $io->...
     });
});
Worker::runAll();
marcosmarcolin commented 3 years ago

I think add timer in socketio process is a better way which codes like this.

$io = new SocketIO($port, $cert);
$io->on('connect', function (){
    // code
});
$io->on('workerStart', function()use($io) {
    Timer::add(5, function () use ($io){
        $io->...
     });
});
Worker::runAll();

Perfect, worked correctly.

I thought I could just create separately, now it's much easier to continue.

Thanks for the quick feedback, hug!