当前位置:网站首页>Swoole定时器
Swoole定时器
2022-07-27 22:38:00 【生命不止、战斗不息】
简介
常规定时器:linux crontab
Swoole定时器
swoole_timer_tick
swoole_timer_after
别名
tick()、after()、clear() 都拥有一个函数风格的别名

应用
Ws.php
<?php
/*ws 优化 基础类库*/
class Ws{
CONST HOST = "0.0.0.0";
CONST PORT = 8812;
public $ws = null;
public function __construct(){
$this->ws = new Swoole\WebSocket\Server("0.0.0.0",8812);
$this->ws->set(
[
# 设置启动的 Worker 进程数。【默认值:CPU 核数】
'worker_num'=>2,
# 配置 Task 进程的数量
'task_worker_num'=>2,
]
);
$this->ws->on("open",[$this,"onOpen"]);
$this->ws->on("message",[$this,"onMessage"]);
$this->ws->on("task",[$this,"onTask"]);
$this->ws->on("finish",[$this,"onFinish"]);
$this->ws->on("close",[$this,"onClose"]);
$this->ws->start();
}
# 监听ws连接事件
public function onOpen($ws,$request)
{
var_dump($request->fd);
if($request->fd==1)
{
/*
tick() 别名函数 swoole_timer_tick
设置一个间隔时钟定时器。
与 after 定时器不同的是 tick 定时器会持续触发,直到调用 Timer::clear 清除。
*/
//每2秒执行
swoole_timer_tick(2000,function($timer_id){
echo "2s:timerId:{$timer_id}\n";
});
}
}
# 监听ws消息事件
public function onMessage($ws,$frame)
{
echo "接收前端发来的信息:{$frame->data}\n";
// todo 10s
$data = [
'task'=>1,
'fd'=>$frame->fd,
];
/*
after()
在指定的时间后执行函数。Swoole\Timer::after 函数是一个一次性定时器,执行完成后就会销毁。
此函数与 PHP 标准库提供的 sleep 函数不同,after 是非阻塞的。而 sleep 调用后会导致当前的进程进入阻塞,将无法处理新的请求。
*/
swoole_timer_after(5000,function() use($ws,$frame)
{
echo "5s-after\n";
$ws->push($frame->fd,"5秒后发送给前台的消息:".date("Y-m-d H:i:s"));
});
$ws->push($frame->fd,"发送给前台的消息:".date("Y-m-d H:i:s"));
}
public function onTask($serv,$taskId,$workerId,$data){
print_r($data);
//耗时场景
sleep(10);
return "on task finish";//告诉work
}
public function onFinish($serv,$taskId,$data){
echo "taskId:{$taskId}\n";
echo "执行完成的异步:{$data}\n";
}
# close
public function onClose($ws,$fd){
echo "client:{$fd}\n";
}
}
$obj=new Ws();
运行服务


index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>client</title>
</head>
<h1>willem-swoole-ws测试</h1>
<script>
var wsUrl = "ws://www.swoole.com:9501";
var websocket=new WebSocket(wsUrl);
//实例对象的onopen属性
websocket.onopen=function(evt){
websocket.send("发送 hello-willem");
console.log("conected-swoole-success");
}
//实例化 onmessage
websocket.onmessage=function(evt){
alert(evt.data+":接收到后端发来的信息弹窗");
console.log("ws-server-return-data:"+evt.data);
}
//onclose
websocket.onclose=function(evt){
console.log("close");
}
//onerror
websocket.onerror=function(evt,e){
console.log("error:"+evt.data);
}
</script>
</body>
</html>
边栏推荐
- CAP的理解
- Valued at $36billion! SpaceX, which is about to launch its first manned launch, raised $346million
- Rancher2.6 monitoring grafana docking LDAP
- 0-1背包问题
- 《KMP复习 + AC自动机》前传
- How to clearly understand and express IAAs, PAAS and SaaS?
- MySQL中的运算符
- How to smoothly go online after MySQL table splitting?
- SRv6初登场
- Examples of application of JMeter in performance testing
猜你喜欢

【OpenCV 例程 300篇】241. 尺度不变特征变换(SIFT)

Jointly create a new chapter in cultural tourism | xinqidian signs a strategic cooperation agreement with Guohua cultural tourism

Use of postman

"C language" deep entry rounding & four functions

One year anniversary of creation, Chongba young Lang

110. SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe
![Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]](/img/6c/d4a45981a7fc87f6a82a91017f8ce8.png)
Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]

Maximize activation
![[300 opencv routines] 241. Scale invariant feature transformation (SIFT)](/img/7a/a764c779c3162920c832325f89f340.png)
[300 opencv routines] 241. Scale invariant feature transformation (SIFT)

Postman download and use tutorial
随机推荐
canvas模拟输入框输入
Demo: the test interface receives duplicate data and creates documents in a short time
Recommendation system model: wide & deep model
Oracle error: ora-01722 invalid number
One year anniversary of creation, Chongba young Lang
Jmeter在性能测试中的应用实践样例
网络设备硬核技术内幕 防火墙与安全网关篇 (七) 虚拟化神器 (上)
推荐系统-离线召回:u2tag2i、icf
leetcode:1997. 访问完所有房间的第一天【跳跃dp】
uniapp显示富文本效果demo(整理)
Border width border fillet border color
Scrollview, tableview nested solutions
Basic use of calculation attributes
Postman下载、使用教程
立即报名 | 云原生技术交流 Meetup 广州站已开启,8 月 6 号与你相遇!
Jerry Zhi doesn't play hidden audio files [article]
Recommended system - indicators: CTR, CVR
Point divide and conquer analysis
[STM32] watchdog module
推荐系统-精排模型:xDeepFM