本文整理汇总了PHP中swoole_server::sendMessage方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_server::sendMessage方法的具体用法?PHP swoole_server::sendMessage怎么用?PHP swoole_server::sendMessage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_server
的用法示例。
在下文中一共展示了swoole_server::sendMessage方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onWorkerStart
public function onWorkerStart(\swoole_server $server, $worker_id)
{
if ($server->taskworker) {
swoole_set_process_name('switch_manage_task_worker');
$server->sendMessage('worker start.', $server->worker_id - 1);
} else {
swoole_set_process_name('switch_manage_event_worker');
if ($worker_id == 0) {
//防止tick被重复启动
$server->tick(self::INTERVAL_PING, function () use($server) {
$server->task('Ping');
});
}
}
}
示例2: function
<?php
$serv = new swoole_server("0.0.0.0", 9501);
$serv->set(array('worker_num' => 2, 'task_worker_num' => 2));
$serv->on('pipeMessage', function ($serv, $src_worker_id, $data) {
echo "#{$serv->worker_id} message from #{$src_worker_id}: {$data}\n";
});
$serv->on('task', function ($serv, $task_id, $from_id, $data) {
var_dump($task_id, $from_id, $data);
$fd = $data;
//$serv->send($fd, str_repeat('B', 1024*rand(40, 60)).rand(10000, 99999)."\n");
});
$serv->on('finish', function ($serv, $fd, $from_id) {
});
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data) {
if (trim($data) == 'task') {
$serv->task("async task coming");
} else {
$worker_id = 1 - $serv->worker_id;
$serv->sendMessage("hello task process", $worker_id);
}
});
$serv->on('close', function ($serv, $fd, $from_id) {
//echo "[#".posix_getpid()."]\tClient@[$fd:$from_id]: Close.\n";
});
$serv->start();
示例3: function
<?php
$serv = new swoole_server("0.0.0.0", 9501, SWOOLE_BASE);
//$serv = new swoole_server("0.0.0.0", 9501);
$serv->set(array('worker_num' => 2, 'task_worker_num' => 2));
$serv->on('pipeMessage', function ($serv, $src_worker_id, $data) {
echo "#{$serv->worker_id} message from #{$src_worker_id}: {$data}\n";
});
$serv->on('task', function (swoole_server $serv, $task_id, $from_id, $data) {
echo "#{$serv->worker_id} NewTask: {$data}\n";
$serv->sendMessage($data, 0);
//$serv->send($fd, str_repeat('B', 1024*rand(40, 60)).rand(10000, 99999)."\n");
});
$serv->on('finish', function ($serv, $fd, $from_id) {
});
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data) {
$cmd = trim($data);
if ($cmd == 'totask') {
$serv->sendMessage("hello task process", 2);
} elseif ($cmd == 'toworker') {
$worker_id = 1 - $serv->worker_id;
$serv->sendMessage("hello worker", $worker_id);
} elseif ($cmd == 'task2worker') {
$serv->task('hello worker from task.');
} else {
echo "#{$serv->worker_id} Recv: {$data}\n";
}
});
$serv->on('close', function ($serv, $fd, $from_id) {
//echo "[#".posix_getpid()."]\tClient@[$fd:$from_id]: Close.\n";
});
示例4: function
<?php
const TASK_N = 1000;
$serv = new swoole_server("127.0.0.1", 9501, SWOOLE_BASE);
$serv->set(['worker_num' => 2]);
$serv->on('connect', function (swoole_server $serv, $fd) {
echo "Client:Connect.\n";
$data = str_repeat("A", 800000);
$serv->sendMessage(array('data' => $data), 1);
});
$serv->on('pipeMessage', function ($serv, $worker_id, $data) {
var_dump($worker_id, is_array($data));
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
//echo "recv n=".strlen($data)."\n";
//$serv->send($fd, 'Swoole: hello');
});
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
$serv->start();
示例5: use
$serv = new swoole_server("0.0.0.0", 9501, $mode, SWOOLE_SOCK_TCP);
$serv->listen('0.0.0.0', 9502, SWOOLE_SOCK_UDP);
$serv->listen('::', 9503, SWOOLE_SOCK_TCP6);
$serv->listen('::', 9504, SWOOLE_SOCK_UDP6);
$process1 = new swoole_process(function ($worker) use($serv) {
global $argv;
swoole_set_process_name("php {$argv[0]}: my_process1");
swoole_timer_tick(2000, function ($interval) use($worker, $serv) {
echo "#{$worker->pid} child process timer {$interval}\n";
// 如果worker中没有定时器,则会输出 process timer xxx
foreach ($serv->connections as $conn) {
$serv->send($conn, "heartbeat\n");
}
});
swoole_timer_tick(5000, function () use($serv) {
$serv->sendMessage("hello event worker", 0);
$serv->sendMessage("hello task worker", 4);
});
}, false);
//$serv->addprocess($process1);
$process2 = new swoole_process(function ($worker) use($serv) {
global $argv;
swoole_set_process_name("php {$argv[0]}: my_process2");
swoole_timer_tick(2000, function ($interval) use($worker, $serv) {
echo "#{$worker->pid} child process timer {$interval}\n";
// 如果worker中没有定时器,则会输出 process timer xxx
});
}, false);
//$serv->addprocess($process2);
$serv->set(G::$config);
/**
示例6: sendMessage
/**
* 发送一条管道通信
* @param \CatLib\Constraint\Network\IPacket $data 数据
* @param bool $taskwork 是否投递到任务线程
* @return void
*/
public function sendMessage(IPacket $data, $taskwork = true)
{
$config = $this->env["swoole"]["config"];
$target = mt_rand($taskwork ? $config["worker_num"] : 0, $taskwork ? $config["worker_num"] + ($config["task_worker_num"] - 1) : $config["worker_num"] - 1);
$this->server->sendMessage($data->serialize(), $target);
}