onMessage

Description:

callback Worker::$onMessage

This is the callback function triggered when data is sent from the client through the connection (when Workerman receives data).

Parameters of the Callback Function

$connection

The connection object, which is an instance of TcpConnection, used to manipulate the client connection, such as sending data, closing the connection, etc.

$data

The data sent from the client connection. If the Worker specifies a protocol, then $data is the decoded data corresponding to that protocol. The data type depends on the implementation of the protocol's decode(), where websocket, text, and frame are strings, and for HTTP protocol, it is an instance of Workerman\Protocols\Http\Request.

Example

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

$worker = new Worker('websocket://0.0.0.0:8484');
$worker->onMessage = function(TcpConnection $connection, $data)
{
    var_dump($data);
    $connection->send('receive success');
};
// Run the worker
Worker::runAll();

Tip: In addition to using anonymous functions as callbacks, you can also refer here for other callback writing methods.