Authored by wuxiao

通过内建方法读取websocket推送服务地址

... ... @@ -3,6 +3,7 @@ namespace backend\controllers;
use Yii;
use backend\components\Pagination;
use yii\helpers\ArrayHelper;
use common\lib\YohoApi\Client as YohoApiClient;
/**
... ... @@ -59,16 +60,25 @@ class ProductController extends BaseController
if (!empty($ret['product_list'])){
foreach ($ret['product_list'] as $product){
$list[$product['product_skn']]->product_name = $product['product_name'];
$list[$product['product_skn']]->sales_price = $product['sales_price'];
if (!empty($list[$product['product_skn']])){
$list[$product['product_skn']]->setAttributes([
'product_name' => $product['product_name'],
'sales_price' => $product['sales_price'],
]);
}
}
}
}
//推送服务地址
$wsAddrs = (new \common\models\SysConfig)->getServerAddr();
$wsAddr = @ArrayHelper::getValue($wsAddrs, ['websocket',0]);
return $this->render('list',[
'room'=> \app\models\Room::findOne(['room_id'=>$room_id]),
'pagination'=>$pagination,
'list'=>$list,
'wsAddr'=>$wsAddr
]);
}
... ...
... ... @@ -10,6 +10,7 @@ $this->registerJsFile('/js/php.js',array('postion'=>View::POS_END));
?>
<script type="text/javascript">
<?php $this->beginBlock('javascript');?>
var wsAddr = '<?=$wsAddr?>';
var i = 0;
var room_id = <?=$room->room_id?>;
var interval_handle;
... ... @@ -20,7 +21,7 @@ var default_interval = 3;
*/
function callWebsocket(callback){
if (!window.ws || window.ws.readyState === undefined || window.ws.readyState != 1) {
websocket('192.168.102.17',9501,'/',function(){
websocket(wsAddr,null,'/',function(){
consoleLog('连接服务器');
callback();
... ... @@ -124,6 +125,9 @@ function getPushlog(interval){
* @returns {undefined}
*/
function consoleLog(msg){
if (msg){
console.log(msg);
}
$('#runtime').append('&nbsp;'+msg+'<br />');
}
/**
... ...
... ... @@ -52,13 +52,13 @@ function websocket(host, port, path, onOpen, onMessage, onClose, onError) {
if (!/:\/\//.test(host)){
host = 'ws://' + host;
}
if (/^:/.test(port)){
if (port && /^:/.test(port)){
port = Number(port.replace(':',''));
}
if (!/^\//.test(path)){
if (path && !/^\//.test(path)){
path = '/' + path;
}
url = host + ':' + port + path;
url = host + (port ? ':' + port : '') + (path ? path : '/');
window.ws = new WebSocket(url);
ws.onopen = (typeof onOpen == 'function') ? onOpen : function(evt)
... ...