walkor / workerman

An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols.
http://www.workerman.net
MIT License
11.03k stars 2.25k forks source link

How to run 2 websockets in different classes? #966

Closed shizzic closed 9 months ago

shizzic commented 9 months ago

I want to have my sockets seperatly, but to run a server i need to use runAll() function. Because of this, starting second server after the first one will crush it and second will be only one working. Example: First.php

class First
{
    private $worker;

    public function actionRun()
    {
        $this->worker = new Worker('websocket://0.0.0.0:8090');
        $this->worker->name = 'websocket1';
        $this->worker->count     = 1;
        $this->worker->reloadable = true;

        $this->worker->onMessage= function ($connection) {
            $connection->send("data");
         }

        Worker::$daemonize  = true;
        $this->worker->runAll();
    }
}

Second.php

class Second
{
    private $worker;

    public function actionRun()
    {
        $this->worker = new Worker('websocket://0.0.0.0:8091');
        $this->worker->name = 'websocket2';
        $this->worker->count     = 1;
        $this->worker->reloadable = true;

        $this->worker->onMessage= function ($connection) {
            $connection->send("data");
         }

        Worker::$daemonize  = true;
        $this->worker->runAll();
    }
}
xpader commented 9 months ago

Run Worker::runAll() after all workers created.