你好,想问下workerman作为客户端连接对方的服务端没有进入onConnect是怎么回事?

西班牙馅饼
  • 6
function subscribe($callback, $sub_str="") {
        $GLOBALS['sub_str'] = $sub_str;
        $GLOBALS['callback'] = $callback;
        $worker = new Worker();
        if(isset(Config::$config['paper'])){
            $url = "";
        }else{
            $url = "";
        }

        try {
            $worker->onWorkerStart = function($worker) use ($url){
                // ssl需要访问443端口
                $con = new AsyncTcpConnection($url);

                $ntime = $this->getTimestamp();
                print_r($ntime." $url\n");

                // 设置以ssl加密方式访问,使之成为wss
                $con->transport = 'ssl';

                // 定时器
                Timer::add(20, function() use ($con)
                {
                    $con->send("ping");
//
                    $ntime = $this->getTimestamp();
                    print_r($ntime." ping\n");
                });


                $con->onConnect = function($con){
                    $data = json_encode([
                        'op' => "subscribe",
//                        'args' => $GLOBALS['sub_str']
                        'args' => [
                            $GLOBALS['sub_str']
                        ]
                    ]);

                    $data = stripslashes($data);
//                    $data = substr($data,1,strlen());
                    $data = '{"op":"subscribe","args":[{'.substr($data,28,-4).'}]}';
                    $ntime = $this->getTimestamp();
                    print_r($ntime . " $data\n");

                    $con->send($data);
//                    $con->send($data);
                };

                $con->onMessage = function($con, $data) {

                    // 如果是深度200档,则校验
                    if(strpos($data,"checksum"))
                    {
                        $ntime = $this->getTimestamp();
                        print_r($ntime . " $data\n");

                        if ($this->partial==null)
                        {
                            $this->partial=$data;
                        }else{
                            $update = $data;
                            // 深度合并
                            $data = $this->checksumTest->depthMerge($this->partial,$update);
                            // 深度校验结果
                            $result = $this->checksumTest->checksum($data);

                            if ($result){
                                print_r(self::getTimestamp()." checksum success\n");
                            } else {
                                die(self::getTimestamp()." checksum fail\n");
                            }

                            print_r("---------------------------------------------------------------\n");
                            // 打印增量数据
                            call_user_func_array($GLOBALS['callback'], array($update));
                            // 更新全局的全量数据
                            $this->partial = $data;
                        }
                    }else{
                        $ntime = $this->getTimestamp();
                        print_r($ntime . " $data\n");
                    }
                };

                $con->onClose = function ($con) {
                    $ntime = $this->getTimestamp();
                    print_r($ntime." reconnecting\n");
                    $con->reConnect(0);
                };

                $con->connect();
            };

            Worker::runAll();
        } catch (\Exception $e) {
            var_dump($e->getMessage());
        }
    }
回复
阅读 103
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏