请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

ws.php中的 onRequest 没有执行

ws.php中的 onRequest 没有执行,在chart.php里面打印var_dump($_POST[‘http_server’])也没有值,前端收不到push的内容
图片描述

<?php
/**
 * Created by ws优化
 * User: dlab-xsy
 * Date: 2019/8/23
 * Time: 1:49 PM
 */

class Ws{

    const HOST = '0.0.0.0';
    const PORT = 9112;
    const CHART_PORT = 8812;

    public $ws = null;
    public function __construct()
    {
        $this->ws = new swoole_websocket_server(self::HOST,self::PORT);

        $this->ws->listen(self::HOST,self::CHART_PORT,SWOOLE_SOCK_TCP);

        $this->ws->set([
            'enable_static_handler' => true,
            'document_root' => '/www/swoole/thinkphp/public/static',
            'worker_num' => 4,
            'task_worker_num' => 4,
        ]);

        $this->ws->on("open", [$this, 'onOpen']);
        $this->ws->on("message", [$this, 'onMessage']);
        $this->ws->on("workerstart", [$this, 'onWorkerStart']);
        $this->ws->on("request", [$this, 'onRequest']);
        $this->ws->on("task", [$this, 'onTask']);
        $this->ws->on("finish", [$this, 'onFinish']);
        $this->ws->on("close", [$this, 'onClose']);

        $this->ws->start();
    }

    //
    public function onWorkerStart($server,$worker_id){
        //定义应用目录
        define('APP_PATH',__DIR__.'/../application/');
        //加载框架里面的文件
//        require __DIR__.'/../thinkphp/start.php';
        require __DIR__ . '/../thinkphp/base.php';
    }

    public function onRequest($request,$response){
        //var_dump('test');
        $_SERVER = [];
        if(isset($request->server)){
            foreach ($request->server as $k => $v){
                $_SERVER[strtoupper($k)] = $v;
            }
        }
        if(isset($request->header)){
            foreach ($request->header as $k => $v){
                $_SERVER[strtoupper($k)] = $v ;
            }
        }

        $_GET = [];
        if(isset($request->get)){
            foreach ($request->get as $k => $v){
                $_GET[$k] = $v;
            }
        }

        $_POST = [];
        if(isset($request->post)){
            foreach ($request->post as $k => $v){
                $_POST[$k] = $v;
            }
        }

        $_FILES = [];
        if(isset($request->files)){
            foreach ($request->files as $k => $v){
                $_FILES[$k] = $v;
            }
        }

        $_POST['http_server'] = $this->ws;

        ob_start();
        try{
//            think\App::run()->send();
            think\Container::get('app',[APP_PATH])
                ->run()
                ->send();
        }catch (Exception $e){
            // todo
        }
        $res = ob_get_contents();
        ob_end_clean();

        $response->end($res);

    }

    //监听ws连接事件
    public function onOpen($ws, $requst){
        var_dump($ws);
        var_dump($requst->fd);
    }

    //监听ws消息事件
    public function onMessage($ws, $frame){
        echo "ser-push-message:{$frame->data}\n";
        $ws->push($frame->fd, "server-push:".date('Y-m-d H:i:s'));
    }

    public function onTask($serv, $taskId, $workerId, $data){
        //分发task任务机制,让不同的任务走不同的逻辑
        $obj = new app\common\lib\task\Task;

        $method = $data['method'];
        $flag = $obj->$method($data['data'],$serv);

        return $flag;
    }

    public function onFinish($serv, $taskId, $data){
        echo "taskId:{$taskId}";
        echo "finish-data-sucess:{$data}";
    }

    //关闭
    public function onClose($ws, $fd){
        echo "clientid:{$fd}\n";
    }

}

$obj = new Ws();

正在回答 回答被采纳积分+3

2回答

singwa 2019-10-31 23:46:20

亲爱的同学您好

https://img1.sycdn.imooc.com//szimg/5dbb01ad0990dcaf11020324.jpg


当您请求一个URL的时候, 在这个onRequest事件回调里面 打印这个字符串 ,能打印出来吗,在终端中查看。

0 回复 有任何疑惑可以回复我~
  • 提问者 木_木_木_木 #1
    打印不出来,小黑框里没有打印test,感觉就是onRequest没有执行
    回复 有任何疑惑可以回复我~ 2019-11-01 10:29:39
  • 提问者 木_木_木_木 #2
    解决了
    回复 有任何疑惑可以回复我~ 2019-11-04 17:16:00
  • singwa 回复 提问者 木_木_木_木 #3
    您好,什么问题导致的?
    回复 有任何疑惑可以回复我~ 2019-11-04 17:16:35
提问者 木_木_木_木 2019-10-31 18:25:39

<?php
namespace app\index\controller;

class Chart
{
   public function index()
   {
       echo 'chart';
       
       var_dump($_POST['http_server']);
       echo '111';
       
       foreach ($_POST['http_server']->ports[1]->connections as $fd){
           $_POST['http_server']->push($fd, $fd);
       }
   }


}

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信