You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pdd-order-api/shell/pmc_message.php

78 lines
2.1 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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();