walkor / workerman

An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols.

Home Page:http://www.workerman.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to run 2 websockets in different classes?

shizzic opened this issue · comments

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();
    }
}

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