<?php
error_reporting(E_ALL);
set_time_limit(0);// 设置超时时间为无限,防止超时
date_default_timezone_set('Asia/shanghai');
header('Content-type:text/html;charset=utf-8');
/*存放websocket客户端*/
$table=new swoole_table(256);
$table->column('name',swoole_table::TYPE_STRING,300);
$table->column('id', swoole_table::TYPE_INT);
$table->create();
/*存放tcp客户端*/
$tcp_table=new swoole_table(256);
$tcp_table->column('name',swoole_table::TYPE_STRING,300);
$tcp_table->column('id', swoole_table::TYPE_INT);
$tcp_table->create();
//主服务
$websocket_server = new swoole_websocket_server("0.0.0.0", 9517,SWOOLE_PROCESS,SWOOLE_SOCK_TCP);
//设置参数
$websocket_server->set(array(
'reactor_num' => 2, //线程数 通过此参数来调节poll线程的数量,以充分利用多核
'worker_num' => 2, //工作进程数
'task_worker_num'=>8, //异步任务工作进程
'backlog' => 128, //listen backlog
'max_request' => 1000, //最大请求书
'log_file'=>'/data/log/swoole.log', //记录swoole服务的错误
'log_level' => 1, //错误级别
// 'dispatch_mode' => 1, //当设置dispatch_mode = 1/3时会自动去掉onConnect/onClose事件回调。
'daemonize' => false, //加入此参数后,执行php server.php将转入后台作为守护进程运行,
'heartbeat_idle_time' => 3600, //允许最大空闲时间
'heartbeat_check_interval' => 3600, //心跳检测间隔
));
//监听端口 9518 协议:tcp 只监听此端口 tcp的链接,发送数据
$tcp_serv = $websocket_server->addListener('127.0.0.1',9518,SWOOLE_SOCK_TCP);
//返回 SwooleServerport 对象,默认集成主服务的协议配置,需要单独调用 SwooleServerPort 的set(),on()方法设置新的协议才会启用新的协议
//
$tcp_serv->set([
"open_eof_check"=>false,
"package_max_length"=>2048,
'heartbeat_check_interval'=>120,
'heartbeat_idle_time'=>1200,
'daemonize' => 1,
]);
//SwooleServerPort::on() 中没有事件 task, finish的回调, 只有connect,receive,close,packet 回调事件
//有新的连接进入时,在worker进程中回调(调用父类swoole_server回调)
$tcp_serv->on('connect', function (swoole_server $server,$fd,