U
U
UntitledNikname2020-05-29 16:44:07
PHP
UntitledNikname, 2020-05-29 16:44:07

Why does Workerman see all the plug-ins as one?

use Workerman\Worker;

require_once __DIR__ . '/vendor/autoload.php';

// #### create socket and listen 1234 port ####
$tcp_worker = new Worker('tcp://0.0.0.0:1234');

// 4 processes
$tcp_worker->count = 4;

// Emitted when new connection come
$tcp_worker->onConnect = function ($connection) {
    echo "New Connection\n";
};

// Emitted when data received
$tcp_worker->onMessage = function ($connection, $data) {
    // Send data to client
    $connection->send("Hello $data \n");
};

// Emitted when new connection come
$tcp_worker->onClose = function ($connection) {
    echo "Connection closed\n";
};

Worker::runAll();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Light Chat, 2020-05-30
@UntitledNikname

Because you are running 4 worker processes, connections are distributed evenly over them, and one worker process ($tcp_worker->connections) has only one connection at that moment. And in the console, the command will return the number of connections for all processes. Moreover, you can’t just send a message to all connections on different workers, so if you don’t need 4 processes in principle, just set $tcp_worker->count = 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question