connections
Description:
array Worker::$connections
The format is
array(id=>connection, id=>connection, ...)
This property stores all the client connection objects of the current process, where id is the connection's id number. For details, see the manual's TcpConnection id property.
$connections is very useful for broadcasting or obtaining connection objects based on connection id.
If the connection's id number is $id, you can easily obtain the corresponding connection object through $worker->connections[$id], and then operate on the corresponding socket connection, for example, to send data using $worker->connections[$id]->send('...').
Note: If a connection is closed (triggers onClose), the corresponding connection will be removed from the $connections array.
Note: Developers should not modify this property, as it may lead to unpredictable situations.
Example
use Workerman\Worker;
use Workerman\Timer;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('text://0.0.0.0:2020');
// Set a timer to send data to all client connections when the process starts
$worker->onWorkerStart = function($worker)
{
// Periodically, every 10 seconds
Timer::add(10, function()use($worker)
{
// Iterate through all client connections of the current process and send the current server time
foreach($worker->connections as $connection)
{
$connection->send(time());
}
});
};
// Run the worker
Worker::runAll();