服务成功执行之后 命令显示 connection not exists

xuanjiangping

如题:帮忙给看看 是什么原因造成的

<?php
use Workerman\Worker;
use \Workerman\Lib\Timer;

require_once '../vendor/workerman/workerman/Autoloader.php';
require_once '../vendor/channel-master/src/Server.php';
require_once '../vendor/channel-master/src/Client.php';

// 初始化一个Channel服务端
$channel_server = new Channel\Server('0.0.0.0', 2206);

// websocket服务端
$worker = new Worker('websocket://0.0.0.0:4236');
$worker->count = 2;
$worker->name = 'pusher';
$worker->onWorkerStart = function ($worker) {
    // Channel客户端连接到Channel服务端
    Channel\Client::connect('127.0.0.1', 2206);
    // 以自己的进程id为事件名称
    $event_name = $worker->id;
    // 订阅worker->id事件并注册事件处理函数
    Channel\Client::on($event_name, function ($event_data) use ($worker) {
        $to_connection_id = $event_data;
        $message = $event_data;
        if (!isset($worker->connections)) {
            echo "connection not exists\n";
            return;
        }
        $to_connection = $worker->connections;
        $to_connection->send($message);
    });

    // 订阅广播事件
    $event_name = '广播';
    // 收到广播事件后向当前进程内所有客户端连接发送广播数据
    Channel\Client::on($event_name, function ($event_data) use ($worker) {
        $message = $event_data;
        foreach ($worker->connections as $connection) {
            $connection->send($message);
        }
    });
};

$worker->onConnect = function ($connection) use ($worker) {
    // $msg = "workerID:{$worker->id} connectionID:{$connection->id} connected\n";
    // echo $msg;
    // $connection->send($msg);
};

$worker->onClose = function ($connection) use ($worker) {
    $sendData = ;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/index.php/v1/closeConnection'); //抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sendData);
    $res = curl_exec($ch);
    curl_close($ch);
    if ($res) {
        $connection->connect();
        $connection->send(json_encode());
    } else {
        $connection->send(json_encode());
    }
};

$worker->onMessage = function ($connection, $data) use ($worker) {
    $data = json_decode($data, true);
    $sendData = ;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/index.php/v1/updateLong'); //抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sendData);
    $res = curl_exec($ch);
    curl_close($ch);
    if ($res) {
        $connection->send(json_encode());
        // 每3秒执行一次
        $time_interval = 10;
        Timer::add($time_interval, function () use ($connection, $data) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/index.php/v1/sendGps'); //抓取指定网页
            curl_setopt($ch, CURLOPT_HEADER, 0);
            // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
            curl_setopt($ch, CURLOPT_TIMEOUT, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
            curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            $res = curl_exec($ch);
            curl_close($ch);
            // if ($res) {
            //     $connection->send($res);
            // }
        });
    } else {
        $connection->send(json_encode());
    }
};

// 用来处理http请求,向任意客户端推送数据,需要传workerID和connectionID
$http_worker = new Worker('http://0.0.0.0:4237');
$http_worker->name = 'publisher';
$http_worker->onWorkerStart = function () {
    Channel\Client::connect('127.0.0.1', 2206);
};
$http_worker->onMessage = function ($connection, $data) {
    $connection->send('ok');
    if (empty($_GET)) {
        return;
    }

    // 是向某个worker进程中某个连接推送数据
    if (isset($_GET) && isset($_GET)) {
        $event_name = $_GET;
        $to_connection_id = $_GET;
        $content = $_GET;
        Channel\Client::publish($event_name, array(
            'to_connection_id' => $to_connection_id,
            'content' => $content,
        ));
    }
    // 是全局广播数据
    else {
        $event_name = '广播';
        $content = $_GET;
        Channel\Client::publish($event_name, array(
            'content' => $content,
        ));
    }
};

Worker::runAll();
3477 0 0
0个回答

年代过于久远,无法发表回答
×
🔝