onMessage
說明:
callback Connection::$onMessage
作用與Worker::$onMessage回調(diào)相同,區(qū)別是只針對當(dāng)前連接有效,也就是可以針對某個(gè)連接的設(shè)置onMessage回調(diào)。
范例
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('websocket://0.0.0.0:8484');
// 當(dāng)有客戶端連接事件時(shí)
$worker->onConnect = function(TcpConnection $connection)
{
// 設(shè)置連接的onMessage回調(diào)
$connection->onMessage = function(TcpConnection $connection, $data)
{
var_dump($data);
$connection->send('receive success');
};
};
// 運(yùn)行worker
Worker::runAll();
上面代碼與下面的效果是一樣的
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('websocket://0.0.0.0:8484');
// 直接設(shè)置所有連接的onMessage回調(diào)
$worker->onMessage = function(TcpConnection $connection, $data)
{
var_dump($data);
$connection->send('receive success');
};
// 運(yùn)行worker
Worker::runAll();