首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
timer doesn't work
代码如下: ```php <?php $serv = new swoole_server("127.0.0.1", 9501); $serv->set(array( 'worker_num' => 1, //'open_eof_check' => true, //'package_eof' => "\r\n", // 'task_worker_num' => 2, //'dispatch_mode' => 2, //'daemonize' => 1, //'heartbeat_idle_time' => 5, //'heartbeat_check_interval' => 5, )); function my_onStart($serv) { global $argv; swoole_set_process_name("php {$argv[0]}: master"); echo "MasterPid={$serv->master_pid}|Manager_pid={$serv->manager_pid}\n"; echo "Server: start.Swoole version is [".SWOOLE_VERSION."]\n"; //$serv->addtimer(1000); } function my_onShutdown($serv) { echo "Server: onShutdown\n"; } function my_onTimer($serv, $interval) { echo "Server:Timer Call.Interval=$interval\n"; } function my_onClose($serv, $fd, $from_id) { //echo "Client: fd=$fd is closed.\n"; } function my_onConnect($serv, $fd, $from_id) { //throw new Exception("hello world"); // echo "Client:Connect.\n"; } function my_onWorkerStart($serv, $worker_id) { global $argv; if($worker_id >= $serv->setting['worker_num']) { swoole_set_process_name("php {$argv[0]}: task_worker"); } else { swoole_set_process_name("php {$argv[0]}: worker"); } //echo "WorkerStart|MasterPid={$serv->master_pid}|Manager_pid={$serv->manager_pid}|WorkerId=$worker_id\n"; //$serv->addtimer(500); //500ms } function my_onWorkerStop($serv, $worker_id) { echo "WorkerStop[$worker_id]|pid=".posix_getpid().".\n"; } function my_onReceive(swoole_server $serv, $fd, $from_id, $data) { $cmd = trim($data); if($cmd == "reload") { $serv->reload($serv); } elseif($cmd == "task") { $task_id = $serv->task("hello world", 0); echo "Dispath AsyncTask: id=$task_id\n"; } elseif($cmd == "taskwait") { $result = $serv->taskwait("hello world"); echo "SyncTask: result=$result\n"; } elseif($cmd == "info") { $info = $serv->connection_info($fd); $serv->send($fd, 'Info: '.var_export($info, true).PHP_EOL); } elseif($cmd == "broadcast") { $start_fd = 0; while(true) { $conn_list = $serv->connection_list($start_fd, 10); if($conn_list === false) { break; } $start_fd = end($conn_list); foreach($conn_list as $conn) { if($conn === $fd) continue; $serv->send($conn, "hello from $fd\n"); } } } //这里故意调用一个不存在的函数 elseif($cmd == "error") { hello_no_exists(); } elseif($cmd == "shutdown") { $serv->shutdown(); } else { $serv->send($fd, 'Swoole: '.$data, $from_id); //$serv->close($fd); } //echo "Client:Data. fd=$fd|from_id=$from_id|data=$data"; //$serv->deltimer(800); //swoole_server_send($serv, $other_fd, "Server: $data", $other_from_id); } function my_onTask(swoole_server $serv, $task_id, $from_id, $data) { echo "AsyncTask[PID=".posix_getpid()."]: task_id=$task_id.".PHP_EOL; $serv->finish("OK"); } function my_onFinish(swoole_server $serv, $data) { echo "AsyncTask Finish:Connect.PID=".posix_getpid().PHP_EOL; } function my_onWorkerError(swoole_server $serv, $data) { echo "worker abnormal exit. WorkerId=$worker_id|Pid=$worker_pid|ExitCode=$exit_code\n"; } $serv->addtimer(200); $serv->on('Start', 'my_onStart'); $serv->on('Connect', 'my_onConnect'); $serv->on('Receive', 'my_onReceive'); $serv->on('Close', 'my_onClose'); $serv->on('Shutdown', 'my_onShutdown'); $serv->on('Timer', 'my_onTimer'); $serv->on('WorkerStart', 'my_onWorkerStart'); $serv->on('WorkerStop', 'my_onWorkerStop'); $serv->on('Task', 'my_onTask'); $serv->on('Finish', 'my_onFinish'); $serv->on('WorkerError', 'my_onWorkerError'); $serv->on('ManagerStart', function($serv) { global $argv; swoole_set_process_name("php {$argv[0]}: manager"); }); $serv->start(); ``` 输出: ```php php -f tcp.php Warning: swoole_server::addtimer(): onTimer is null, Can not use timer. in /projects/test/swoole-test/tcp.php on line 136 MasterPid=32941|Manager_pid=32942 Server: start.Swoole version is [1.7.6] ```
发布于8年前 · 0 次浏览 · 来自
提问
新
新用户(手机注册)
代码如下: ```php <?php $serv = new swoole_server("127.0.0.1", 9501); $serv->set(array( 'worker_num' => 1, //'open_eof_check' => true, //'package_eof' => "\r\n", // 'task_worker_num' => 2, //'dispatch_mode' => 2, //'daemonize' => 1, //'heartbeat_idle_time' => 5, //'heartbeat_check_interval' => 5, )); function my_onStart($serv) { global $argv; swoole_set_process_name("php {$argv[0]}: master"); echo "MasterPid={$serv->master_pid}|Manager_pid={$serv->manager_pid}\n"; echo "Server: start.Swoole version is [".SWOOLE_VERSION."]\n"; //$serv->addtimer(1000); } function my_onShutdown($serv) { echo "Server: onShutdown\n"; } function my_onTimer($serv, $interval) { echo "Server:Timer Call.Interval=$interval\n"; } function my_onClose($serv, $fd, $from_id) { //echo "Client: fd=$fd is closed.\n"; } function my_onConnect($serv, $fd, $from_id) { //throw new Exception("hello world"); // echo "Client:Connect.\n"; } function my_onWorkerStart($serv, $worker_id) { global $argv; if($worker_id >= $serv->setting['worker_num']) { swoole_set_process_name("php {$argv[0]}: task_worker"); } else { swoole_set_process_name("php {$argv[0]}: worker"); } //echo "WorkerStart|MasterPid={$serv->master_pid}|Manager_pid={$serv->manager_pid}|WorkerId=$worker_id\n"; //$serv->addtimer(500); //500ms } function my_onWorkerStop($serv, $worker_id) { echo "WorkerStop[$worker_id]|pid=".posix_getpid().".\n"; } function my_onReceive(swoole_server $serv, $fd, $from_id, $data) { $cmd = trim($data); if($cmd == "reload") { $serv->reload($serv); } elseif($cmd == "task") { $task_id = $serv->task("hello world", 0); echo "Dispath AsyncTask: id=$task_id\n"; } elseif($cmd == "taskwait") { $result = $serv->taskwait("hello world"); echo "SyncTask: result=$result\n"; } elseif($cmd == "info") { $info = $serv->connection_info($fd); $serv->send($fd, 'Info: '.var_export($info, true).PHP_EOL); } elseif($cmd == "broadcast") { $start_fd = 0; while(true) { $conn_list = $serv->connection_list($start_fd, 10); if($conn_list === false) { break; } $start_fd = end($conn_list); foreach($conn_list as $conn) { if($conn === $fd) continue; $serv->send($conn, "hello from $fd\n"); } } } //这里故意调用一个不存在的函数 elseif($cmd == "error") { hello_no_exists(); } elseif($cmd == "shutdown") { $serv->shutdown(); } else { $serv->send($fd, 'Swoole: '.$data, $from_id); //$serv->close($fd); } //echo "Client:Data. fd=$fd|from_id=$from_id|data=$data"; //$serv->deltimer(800); //swoole_server_send($serv, $other_fd, "Server: $data", $other_from_id); } function my_onTask(swoole_server $serv, $task_id, $from_id, $data) { echo "AsyncTask[PID=".posix_getpid()."]: task_id=$task_id.".PHP_EOL; $serv->finish("OK"); } function my_onFinish(swoole_server $serv, $data) { echo "AsyncTask Finish:Connect.PID=".posix_getpid().PHP_EOL; } function my_onWorkerError(swoole_server $serv, $data) { echo "worker abnormal exit. WorkerId=$worker_id|Pid=$worker_pid|ExitCode=$exit_code\n"; } $serv->addtimer(200); $serv->on('Start', 'my_onStart'); $serv->on('Connect', 'my_onConnect'); $serv->on('Receive', 'my_onReceive'); $serv->on('Close', 'my_onClose'); $serv->on('Shutdown', 'my_onShutdown'); $serv->on('Timer', 'my_onTimer'); $serv->on('WorkerStart', 'my_onWorkerStart'); $serv->on('WorkerStop', 'my_onWorkerStop'); $serv->on('Task', 'my_onTask'); $serv->on('Finish', 'my_onFinish'); $serv->on('WorkerError', 'my_onWorkerError'); $serv->on('ManagerStart', function($serv) { global $argv; swoole_set_process_name("php {$argv[0]}: manager"); }); $serv->start(); ``` 输出: ```php php -f tcp.php Warning: swoole_server::addtimer(): onTimer is null, Can not use timer. in /projects/test/swoole-test/tcp.php on line 136 MasterPid=32941|Manager_pid=32942 Server: start.Swoole version is [1.7.6] ```
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2016-02-14
m
mouse
不能在外部添加定时器,请在回调函数中使用addtimer
赞
0
回复
微信公众号
热门内容
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。