Swoole-WebSocket毫秒定时器

Posted willem_chen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swoole-WebSocket毫秒定时器相关的知识,希望对你有一定的参考价值。

Swoole毫秒定时器

简介

常规定时器:linux crontab

Swoole定时器
swoole_timer_tick
swoole_timer_after

别名

tick()、after()、clear() 都拥有一个函数风格的别名

类静态方法函数风格别名
Swoole\\Timer::tick()swoole_timer_tick()
Swoole\\Timer::after()swoole_timer_after()
Swoole\\Timer::clear()swoole_timer_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://test.cc:8812";
  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>

以上是关于Swoole-WebSocket毫秒定时器的主要内容,如果未能解决你的问题,请参考以下文章

c++代码执行定时器返回0,需要以毫秒为单位输出

Sleep() 方法后的代码片段没有被执行

使用Swoole实现毫秒级定时任务

关于定时器的那些事!

我可以使用 Excel for Mac 将代码计时到毫秒吗

使用系统定时器SysTick实现精确延时微秒和毫秒函数