//创建Server对象,监听 127.0.0.1:9501端口
$serv = new swoole_server("127.0.0.1", 9501);
$serv->set(array(
'reactor_num' => 2, //reactor线程数
'worker_num' => 4, //worker 进程数
'backlog' => 128, //listen backlog
'max_request' => 2,
'dispatch_mode' => 1,
));
//监听连接进入事件
/**
*$fd 客户端连接的唯一标示
* $reactor_id 线程id
*/
$serv->on('connect', function ($serv, $fd,$reactor_id) {
echo "Client: {$reactor_id} - {$fd} Connect.\n";
});
//监听数据接收事件
$serv->on('receive', function ($serv, $fd, $reactor_id, $data) {
$serv->send($fd, "Server: {$reactor_id} - {$fd} ".$data);
});
//监听连接关闭事件
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
//启动服务器
$serv->start();
// telnet 127.0.0.1 9501
网友评论