06.swoole学习笔记--异步tcp服务器

<?php
//创建tcp服务器
$host='0.0.0.0';
$port=9501;
$serv=new swoole_server($host,$port);
//设置异步进程工作数
$serv->set(array('task_worker_num'=>4));
//投递异步任务
$serv->on('receive',function($serv,$fd,$from_id,$data){
    $task_id=$serv->task($data);
    echo "异步ID:$task_id\n";
});

//处理异步任务
$serv->on('task',function($serv,$task_id,$from_id,$data){
    echo "执行异步ID:$task_id";
    $serv->finish("$data -> OK");
});

//处理结果
$serv->on('finish',function($serv,$task_id,$data){
    echo "执行完成";
});

//启动服务器
$serv->start(); 
//php index.php
//ps -ajft              //查看启动进程
//service iptables stop //关闭防火墙
?>

 

 

posted @ 2018-02-11 13:07  邹柯  阅读(217)  评论(0编辑  收藏  举报