How to Send Data to a Specific Client in Workerman

Using worker as the server without GatewayWorker, how to push messages to a specific user?

<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

// Initialize a worker container, listening on port 1234
$worker = new Worker('websocket://workerman.net:1234');
// ==== The number of processes must be set to 1 ====
$worker->count = 1;
// Add a property to save the mapping of uid to connection (uid is the user id or client unique identifier)
$worker->uidConnections = array();
// Callback function executed when a client sends a message
$worker->onMessage = function(TcpConnection $connection, $data)
{
    global $worker;
    // Check if the current client has been authenticated, i.e., whether uid has been set
    if(!isset($connection->uid))
    {
       // If not authenticated, treat the first package as uid (for demonstration purposes, no real authentication is performed)
       $connection->uid = $data;
       /* Save the mapping of uid to connection, so that connection can be easily found through uid,
        * achieving targeted data push to a specific uid
        */
       $worker->uidConnections[$connection->uid] = $connection;
       return $connection->send('login success, your uid is ' . $connection->uid);
    }
    // Other logic, sending to a specific uid or global broadcasting
    // Assume the message format is uid:message, where it sends message to uid
    // uid as all means global broadcast
    list($recv_uid, $message) = explode(':', $data);
    // Global broadcast
    if($recv_uid == 'all')
    {
        broadcast($message);
    }
    // Send to a specific uid
    else
    {
        sendMessageByUid($recv_uid, $message);
    }
};

// When a client connection is closed
$worker->onClose = function(TcpConnection $connection)
{
    global $worker;
    if(isset($connection->uid))
    {
        // Delete mapping when connection is closed
        unset($worker->uidConnections[$connection->uid]);
    }
};

// Push data to all authenticated users
function broadcast($message)
{
   global $worker;
   foreach($worker->uidConnections as $connection)
   {
        $connection->send($message);
   }
}

// Push data to a specific uid
function sendMessageByUid($uid, $message)
{
    global $worker;
    if(isset($worker->uidConnections[$uid]))
    {
        $connection = $worker->uidConnections[$uid];
        $connection->send($message);
    }
}

// Run all workers (currently only one is defined)
Worker::runAll();

Note:

The above example can push to a specific uid. Although it is a single process, it can still support around 100,000 online connections.

Please note that this example can only be single process, which means that $worker->count must be 1. To support multi-process or server clusters, you need the Channel component to complete inter-process communication. The development is very simple; you can refer to the Channel component cluster push example section.

If you want to push messages to clients in other systems, you can refer to the section on pushing in other projects