zoukankan      html  css  js  c++  java
  • swoole学习(四):websocket

    相关事件:https://wiki.swoole.com/#/websocket_server

    <?php
    /**
     * websocket基础类库
     */
    
    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_num' => 2,
                    '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连接事件
         * @param $ws
         * @param $request
         */
        public function onOpen($ws, $request) {
            var_dump($request->fd);
            if($request->fd == 1) {
                // 每2秒执行
                swoole_timer_tick(2000, function($timer_id){
                    echo "2s: timerId:{$timer_id}
    ";
                });
            }
        }
    
        /**
         * 监听ws消息事件
         * @param $ws
         * @param $frame
         */
        public function onMessage($ws, $frame) {
            echo "ser-push-message:{$frame->data}
    ";
            // todo 10s
            $data = [
                'task' => 1,
                'fd' => $frame->fd,
            ];
            //$ws->task($data);
    
            swoole_timer_after(5000, function() use($ws, $frame) {
                echo "5s-after
    ";
                $ws->push($frame->fd, "server-time-after:");
            });
            $ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s"));
        }
    
        /**
         * @param $serv
         * @param $taskId
         * @param $workerId
         * @param $data
         */
        public function onTask($serv, $taskId, $workerId, $data) {
            print_r($data);
            // 耗时场景 10s
            sleep(10);
            return "on task finish"; // 告诉worker
        }
    
        /**
         * @param $serv
         * @param $taskId
         * @param $data
         */
        public function onFinish($serv, $taskId, $data) {
            echo "taskId:{$taskId}
    ";
            echo "finish-data-sucess:{$data}
    ";
        }
    
        /**
         * close
         * @param $ws
         * @param $fd
         */
        public function onClose($ws, $fd) {
            echo "clientid:{$fd}
    ";
        }
    }
    
    $obj = new Ws();

    可以使用 Chrome 浏览器进行测试,JS 代码为:

    var wsServer = 'ws://127.0.0.1:9502';
    var websocket = new WebSocket(wsServer);
    websocket.onopen = function (evt) {
        console.log("Connected to WebSocket server.");
    };
    
    websocket.onclose = function (evt) {
        console.log("Disconnected");
    };
    
    websocket.onmessage = function (evt) {
        console.log('Retrieved data from server: ' + evt.data);
    };
    
    websocket.onerror = function (evt, e) {
        console.log('Error occured: ' + evt.data);
    };
    慢慢来才是最快的
  • 相关阅读:
    Zend Studio使用
    iOS中block实现的探究
    用python演示一个简单的AST(抽象语法树)
    Cocos2D-x权威指南: CCNode类方法:
    ListView的优化
    可变參数
    android媒体--stagefright概述【一】
    flume安装及配置
    linux包之sysstat之mpstat与pidstat命令
    Java实现第十届蓝桥杯等差数列
  • 原文地址:https://www.cnblogs.com/jongty/p/12763855.html
Copyright © 2011-2022 走看看