作为程序员一定要保持良好的睡眠,才能好编程

Swoole 学习笔记 -- 异步网络通信(2)

发布时间:2018-04-11

Swoole学习(2)



Swoole Task 任务的使用

使用场景:  执行耗时的操作 比如 发送邮件、广播 等。   如果一直在等待,会非常的耗时,对用户体验非常不好。


放在task 中的任务 不会影响前台的使用。




那么如何使用Task 任务呢? 需要设置两个函数


onTask


onFinish

-------------------------

还必须要设置

worker_num=>2

task_worker_num=>2




可以在httpserver中使用,也可以 webSocket中使用 。 并不是只有在webSocket中使用。


onMessage 当我们收到客户端消息的时候,会触发我们业务逻辑

public functio onMessage($server,$frame){

    //处理业务逻辑,比如说发送邮件

    等待10秒


}

如果发送邮件有10秒,那么我们客户端  webSocket 的onmessage 就需要等待10秒后才会返回。

这样的用户体验非常不好。



task 实例代码:


ws_server_task.php

class Ws{
    public $server=null;
    public $setAttrs=[];
    CONST HOST="0.0.0.0";
    const PORT=9999;
    public function __construct(){
        $this->server=new swoole_websocket_server(self::HOST,self::PORT);
        $this->server->on("task",[$this,"onTask"]);
        $this->server->on("finish",[$this,"onFinish"]);
        $this->server->on("open",[$this,"onOpen"]);
        $this->server->on("message",[$this,"onMessage"]);
        $this->server->on("close",[$this,"onClose"]);
    }

    public function onOpen($server,$frame){
        echo $frame->fd."---连接成功\n";
    }
        
    public function onMessage($server,$frame){
        echo $frame->fd."----发来消息说:".$frame->data."\n";
        $_dataAttr=[
            'task'=>1,
            'fd'=>$frame->fd
        ];
        $server->task($_dataAttr);
        $server->push($frame->fd,"服务器返回消息说:".$frame->data."--".date("Y-m-d H:i:s"));
    }

    public function onClose($server,$fd){
        echo $fd." 客户端已经关闭";
    }

    public function onTask($server,$taskId,$workerId,$data){
       print_r($data);
        echo date("Y-m-d H:i:s")."\n";

        //var_dump($server,$taskId,$workerId);  

        echo "tackId\n";
        var_dump($taskId);
        echo "tackId\n";

        echo "workerId\n";
        var_dump($workerId);
        echo "workerId\n";

        $server->push($data['fd'],"task任务执行了".date("Y-m-d H:i:s"));
        sleep(10);

        $server->push($data['fd'],"task   ----10秒后  任务执行了".date("Y-m-d H:i:s"));

        echo "执行完毕 ".date("Y-m-d H:i:s")."\n";
        return "在".date("Y-m-d H:i:s")."执行结束了";
    }

    public function onFinish($server,$taskId,$data){

        echo $taskId."  ---".$data."\n";

    }

    /**
     * @param $key worker_num
     * @param $val 2
     */
    public function set($key,$val){
        $this->setAttrs[$key]=$val;
    }

    //启动服务
    public function start(){
        $this->server->set($this->setAttrs);
        $this->server->start();
    }

}

$_ws=new Ws();
$_ws->set("worker_num",2);
$_ws->set("task_worker_num",2);
$_ws->start();


client.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        #container{
            overflow-y: scroll;
        }
        #container div{
            font-size: 14px;
            color:#666;
            padding: 5px 0px;
            margin:0px auto;
            width:95%;

        }
    </style>

    <script type="text/javascript">

        window.onload=function(){
            var container=document.getElementById("container");
            var submitButton=document.getElementById("submit");
            var message=null;
            var webSocket=new WebSocket("ws://192.168.61.103:9999");

            webSocket.onopen=function(event){
                webSocket.send("你好!");
                console.log("服务器连接成功...");
            }

            webSocket.onmessage=function(event){
                console.log("服务器回复说:"+event.data);
                container.appendChild(createDiv(event.data));
            }

            webSocket.onclose=function(event){
                console.log("connect close");
            }

            submitButton.onclick=function(){
                message=document.getElementById("message").value;
                if(message){
                    webSocket.send(message);
                    document.getElementById("message").value="";
                }else{
                    alert("请输入内容后,点击发送");
                }
            }

            function createDiv(message){
                var div=document.createElement("div");
                var textNode=document.createTextNode(message);
                div.appendChild(textNode);
                return div;
            }
        }
    </script>
</head>
<body>
<div id="container" style="border:1px solid #ccc; height:360px;width:600px;">

</div>
<input type="text" id="message"><input type="button" id="submit" value="发送">

</body>
</html>


截图07.png


运行结果如上图所示。


但是我不清楚的是:

截图08.png


这是我们传递到task任务的参数,


task 的值设置为1  ,我们可以通过 task 这个标识,执行不同的任务。