发布于 2015-08-09 09:38:27 | 591 次阅读 | 评论: 0 | 来源: 网络整理
此函数可以向任意worker进程或者task进程发送消息。在非主进程和管理进程中可调用。收到消息的进程会触发onPipeMessage事件。
bool swoole_server->sendMessage(string $message, int $dst_worker_id);
sendMessage接口在swoole-1.7.9以上版本可用
使用sendMessage必须注册onPipeMessage事件回调函数
$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: $datan";
});
$serv->on('task', function ($serv, $task_id, $from_id, $data){
var_dump($task_id, $from_id, $data);
});
$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->start();