如題:幫忙給看看 是什么原因造成的
<?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';
// 初始化一個(gè)Channel服務(wù)端
$channel_server = new Channel\Server('0.0.0.0', 2206);
// websocket服務(wù)端
$worker = new Worker('websocket://0.0.0.0:4236');
$worker->count = 2;
$worker->name = 'pusher';
$worker->onWorkerStart = function ($worker) {
// Channel客戶端連接到Channel服務(wù)端
Channel\Client::connect('127.0.0.1', 2206);
// 以自己的進(jìn)程id為事件名稱
$event_name = $worker->id;
// 訂閱worker->id事件并注冊(cè)事件處理函數(shù)
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 = '廣播';
// 收到廣播事件后向當(dāng)前進(jìn)程內(nèi)所有客戶端連接發(fā)送廣播數(shù)據(jù)
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'); //抓取指定網(wǎng)頁(yè)
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); //要求結(jié)果為字符串且輸出到屏幕上
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'); //抓取指定網(wǎng)頁(yè)
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); //要求結(jié)果為字符串且輸出到屏幕上
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秒執(zhí)行一次
$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'); //抓取指定網(wǎng)頁(yè)
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); //要求結(jié)果為字符串且輸出到屏幕上
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請(qǐng)求,向任意客戶端推送數(shù)據(jù),需要傳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;
}
// 是向某個(gè)worker進(jìn)程中某個(gè)連接推送數(shù)據(jù)
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,
));
}
// 是全局廣播數(shù)據(jù)
else {
$event_name = '廣播';
$content = $_GET;
Channel\Client::publish($event_name, array(
'content' => $content,
));
}
};
Worker::runAll();