|
|
<?php
|
|
|
|
|
|
// 应用目录
|
|
|
$rootDir = dirname(dirname(__FILE__));
|
|
|
$appDir = 'app';
|
|
|
require $rootDir . '/vendor/autoload.php';
|
|
|
|
|
|
ini_set('default_charset', 'utf-8');
|
|
|
// 启动MVC
|
|
|
include $rootDir . '/zc-framework/zc.php';
|
|
|
Zc::init($rootDir, $appDir);
|
|
|
|
|
|
use Workerman\Worker;
|
|
|
use Workerman\Lib\Timer;
|
|
|
use Workerman\Connection\AsyncTcpConnection;
|
|
|
|
|
|
$worker = new Worker();
|
|
|
$pddMessageService = new PmcService();
|
|
|
$con = null;
|
|
|
|
|
|
function connect($pddMessageService) {
|
|
|
$address = $pddMessageService->buildWsAddress();
|
|
|
// ssl需要访问443端口
|
|
|
$con = new AsyncTcpConnection($address);
|
|
|
// 设置以ssl加密方式访问,使之成为wss
|
|
|
if (!AppConst::isDuoDuoCloud()) {
|
|
|
$con->transport = 'ssl';
|
|
|
}
|
|
|
|
|
|
$con->onConnect = function($con) use ($pddMessageService) {
|
|
|
$pddMessageService->log('onConnect');
|
|
|
};
|
|
|
|
|
|
$con->onMessage = function($con, $data) use ($pddMessageService) {
|
|
|
$pddMessageService->log('onMessage:' . $data);
|
|
|
$retMsg = $pddMessageService->onPmcMessage($data);
|
|
|
if ($retMsg !== false) {
|
|
|
$con->send($retMsg);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
$con->onClose = function($con) use ($pddMessageService) {
|
|
|
$pddMessageService->log('onClose');
|
|
|
};
|
|
|
|
|
|
$con->connect();
|
|
|
|
|
|
return $con;
|
|
|
}
|
|
|
|
|
|
$worker->onWorkerStart = function($worker) use ($pddMessageService) {
|
|
|
global $con;
|
|
|
$con = connect($pddMessageService);
|
|
|
|
|
|
$time_interval = 10;
|
|
|
Timer::add($time_interval, function() use($pddMessageService) {
|
|
|
global $con;
|
|
|
if (empty($con)) {
|
|
|
$pddMessageService->log('timer connect is null');
|
|
|
return;
|
|
|
}
|
|
|
$currentStatus = $con->getStatus(false);
|
|
|
$pddMessageService->log('timer:' . $currentStatus);
|
|
|
|
|
|
if (!in_array($currentStatus, ['ESTABLISHED', 'CONNECTING' ,'CLOSING'])) {
|
|
|
$pddMessageService->log('reconnect');
|
|
|
$con = connect($pddMessageService);
|
|
|
} else if ($currentStatus == 'ESTABLISHED'){
|
|
|
$ret = $con->send($pddMessageService->buildHeartBeatMessage());
|
|
|
$pddMessageService->log('send heartbeat ret' . print_r($ret, true));
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
|
|
|
Worker::runAll();
|
|
|
|
|
|
|