Showing
5 changed files
with
147 additions
and
66 deletions
@@ -18,4 +18,13 @@ class AjaxController extends BaseController | @@ -18,4 +18,13 @@ class AjaxController extends BaseController | ||
18 | $log = Yii::$app->redisIm->rpop(Yii::$app->params['cache_prefix'].'push_goods_log'); | 18 | $log = Yii::$app->redisIm->rpop(Yii::$app->params['cache_prefix'].'push_goods_log'); |
19 | $this->renderJson(Yii::$app->params['success_code'],'',(string)$log); | 19 | $this->renderJson(Yii::$app->params['success_code'],'',(string)$log); |
20 | } | 20 | } |
21 | + | ||
22 | + /** | ||
23 | + * 将房间结束直播 | ||
24 | + */ | ||
25 | + public function actionStopliving() | ||
26 | + { | ||
27 | + $room_id = Yii::$app->request->get('room_id'); | ||
28 | + $this->renderJson(Yii::$app->params['success_code'],'',$room_id); | ||
29 | + } | ||
21 | } | 30 | } |
@@ -70,15 +70,10 @@ class ProductController extends BaseController | @@ -70,15 +70,10 @@ class ProductController extends BaseController | ||
70 | } | 70 | } |
71 | } | 71 | } |
72 | 72 | ||
73 | - //推送服务地址 | ||
74 | - $wsAddrs = (new \common\models\SysConfig)->getServerAddr(); | ||
75 | - $wsAddr = @ArrayHelper::getValue($wsAddrs, ['websocket',0]); | ||
76 | - | ||
77 | return $this->render('list',[ | 73 | return $this->render('list',[ |
78 | 'room'=> \app\models\Room::findOne(['room_id'=>$room_id]), | 74 | 'room'=> \app\models\Room::findOne(['room_id'=>$room_id]), |
79 | 'pagination'=>$pagination, | 75 | 'pagination'=>$pagination, |
80 | 'list'=>$list, | 76 | 'list'=>$list, |
81 | - 'wsAddr'=>$wsAddr | ||
82 | ]); | 77 | ]); |
83 | } | 78 | } |
84 | 79 |
backend/views/layouts/websocket.php
0 → 100644
1 | +<?php | ||
2 | +use yii\web\View; | ||
3 | +use yii\helpers\ArrayHelper; | ||
4 | + | ||
5 | +$this->registerJsFile('/js/php.js',array('postion'=>View::POS_END)); | ||
6 | +$this->registerJsFile('/js/common.js',array('position'=>View::POS_END)); | ||
7 | +?> | ||
8 | + | ||
9 | +<?php | ||
10 | +//推送服务地址 | ||
11 | +$wsAddrs = (new \common\models\SysConfig)->getServerAddr(); | ||
12 | +$wsAddr = @ArrayHelper::getValue($wsAddrs, ['websocket',0]); | ||
13 | +?> | ||
14 | + | ||
15 | +<script type="text/javascript"> | ||
16 | +<?php $this->beginBlock('javascript');?> | ||
17 | +var socket_arr = '<?=$wsAddr?>'.split(":"); | ||
18 | +var wsAddr = socket_arr[0]; | ||
19 | +var port = socket_arr[1]; | ||
20 | + | ||
21 | +/** | ||
22 | + * websocket推送指令 | ||
23 | + * @returns {undefined} | ||
24 | + */ | ||
25 | +function callWebsocket(sendCallback,afterSendCallback = function(){},errorSendCallback = function(){}){ | ||
26 | + if (!window.ws || window.ws.readyState === undefined || window.ws.readyState != 1) { | ||
27 | + websocket(wsAddr,port,'/',function(){ | ||
28 | + consoleLog('连接服务器'); | ||
29 | + | ||
30 | + sendCallback(); | ||
31 | + },function(evt){ | ||
32 | + consoleLog('收到消息: '+evt.data); | ||
33 | + data = JSON.parse(evt.data); | ||
34 | + if (data.status == 1){ | ||
35 | + consoleLog('请求推送成功'); | ||
36 | + afterSendCallback(); | ||
37 | + }else{ | ||
38 | + consoleLog('请求推送失败'); | ||
39 | + errorSendCallback(); | ||
40 | + } | ||
41 | + },function(){ | ||
42 | + consoleLog('连接已关闭'); | ||
43 | + },function(){ | ||
44 | + consoleLog('有错误出现'); | ||
45 | + }); | ||
46 | + }else{ | ||
47 | + sendCallback(); | ||
48 | + } | ||
49 | +} | ||
50 | +/** | ||
51 | + * 页面打印发送记录 | ||
52 | + * @param {type} msg | ||
53 | + * @returns {undefined} | ||
54 | + */ | ||
55 | +function consoleLog(msg){ | ||
56 | + if (msg){ | ||
57 | + console.log(msg); | ||
58 | + } | ||
59 | + if ($('#wsRuntime').size()){ | ||
60 | + $('#wsRuntime').append(' '+msg+'<br />'); | ||
61 | + } | ||
62 | +} | ||
63 | +/** | ||
64 | + * 密钥生成 | ||
65 | + * @param {type} package | ||
66 | + * @returns {unresolved} | ||
67 | + */ | ||
68 | +function makeSign(package){ | ||
69 | + package['private_key'] = 'a85bb0674e08986c6b115d5e3a4884fa'; | ||
70 | + package = ksort(package); | ||
71 | + packageList = []; | ||
72 | + for (key in package){ | ||
73 | + packageList.push(trim(key + '=' + package[key])); | ||
74 | + } | ||
75 | + package['secret'] = strtolower(md5(implode('&', packageList))); | ||
76 | + delete(package['private_key']); | ||
77 | + return package; | ||
78 | +} | ||
79 | +<?php | ||
80 | +$this->endBlock(); | ||
81 | +$this->registerJs($this->blocks['javascript'],View::POS_END) | ||
82 | +?> | ||
83 | +</script> |
1 | <?php | 1 | <?php |
2 | +use yii\web\View; | ||
2 | use yii\helpers\Html; | 3 | use yii\helpers\Html; |
3 | use backend\widgets\LinkPager; | 4 | use backend\widgets\LinkPager; |
4 | use backend\widgets\YHGImage\Common\Images as CommonImages; | 5 | use backend\widgets\YHGImage\Common\Images as CommonImages; |
@@ -6,6 +7,48 @@ use backend\widgets\YHGImage\Common\Images as CommonImages; | @@ -6,6 +7,48 @@ use backend\widgets\YHGImage\Common\Images as CommonImages; | ||
6 | $this->title = $this->params['main_title'].'-'.$this->params['sub_title']; | 7 | $this->title = $this->params['main_title'].'-'.$this->params['sub_title']; |
7 | ?> | 8 | ?> |
8 | 9 | ||
10 | +<?php echo $this->render('@app/views/layouts/websocket');?> | ||
11 | + | ||
12 | +<script type="text/javascript"> | ||
13 | +<?php $this->beginBlock('javascript');?> | ||
14 | +/** | ||
15 | + * 推送房间结束直播指令 | ||
16 | + * @returns {undefined} | ||
17 | + */ | ||
18 | +function stopLiving(room_id){ | ||
19 | + if (!confirm('将房间设为直播结束状态,确定?')){ | ||
20 | + return; | ||
21 | + } | ||
22 | + | ||
23 | + callWebsocket(function(){ | ||
24 | + consoleLog(''); | ||
25 | + consoleLog('发起房间结束直播指令'); | ||
26 | + | ||
27 | + cmd = {cmd:11,room:room_id,msg:'',videoLen:'10:23'}; | ||
28 | + cmd = makeSign(cmd); | ||
29 | + | ||
30 | + cmd = JSON.stringify(cmd); | ||
31 | + consoleLog('发送消息: '+cmd); | ||
32 | + ws.send(cmd); | ||
33 | + alert('指令已发送'); | ||
34 | + },function(){ | ||
35 | + $.get('/ajax/stopliving',{room_id:room_id},function(json){ | ||
36 | + console.log(json); | ||
37 | + if (json.data){ | ||
38 | + consoleLog('监听到推送记录: '+json.data); | ||
39 | + } | ||
40 | + },'json'); | ||
41 | + alert('推送成功'); | ||
42 | + },function(){ | ||
43 | + alert('推送失败!'); | ||
44 | + }); | ||
45 | +} | ||
46 | +<?php | ||
47 | +$this->endBlock(); | ||
48 | +$this->registerJs($this->blocks['javascript'],View::POS_END) | ||
49 | +?> | ||
50 | +</script> | ||
51 | + | ||
9 | <div class="panel panel-default"> | 52 | <div class="panel panel-default"> |
10 | <div class="panel-body"> | 53 | <div class="panel-body"> |
11 | <form action="" method="GET" class="form-inline"> | 54 | <form action="" method="GET" class="form-inline"> |
@@ -63,6 +106,12 @@ $this->title = $this->params['main_title'].'-'.$this->params['sub_title']; | @@ -63,6 +106,12 @@ $this->title = $this->params['main_title'].'-'.$this->params['sub_title']; | ||
63 | </td> | 106 | </td> |
64 | <td> | 107 | <td> |
65 | 108 | ||
109 | + <span> | ||
110 | + <?php if ($model->living == 1)://直播中?> | ||
111 | + <button type="button" onclick="stopLiving(<?=$model->room_id?>)" class="btn btn-danger btn-metro">结束直播</button> | ||
112 | + <?php endif;?> | ||
113 | + </span> | ||
114 | + | ||
66 | <a href="/live/roomedit/<?=$model->id?>" class="btn btn-default btn-white">编辑</a> | 115 | <a href="/live/roomedit/<?=$model->id?>" class="btn btn-default btn-white">编辑</a> |
67 | 116 | ||
68 | <a href="/forbidden/index?room_id=<?=$model->room_id?>" class="btn btn-default btn-white">禁言</a> | 117 | <a href="/forbidden/index?room_id=<?=$model->room_id?>" class="btn btn-default btn-white">禁言</a> |
@@ -70,7 +119,7 @@ $this->title = $this->params['main_title'].'-'.$this->params['sub_title']; | @@ -70,7 +119,7 @@ $this->title = $this->params['main_title'].'-'.$this->params['sub_title']; | ||
70 | <a href="/product/list/<?=$model->room_id?>" class="btn btn-default btn-white">商品</a> | 119 | <a href="/product/list/<?=$model->room_id?>" class="btn btn-default btn-white">商品</a> |
71 | 120 | ||
72 | <span> | 121 | <span> |
73 | - <?php if ($model->status == 0)://未开始?> | 122 | + <?php if ($model->status == 0)://关闭状态?> |
74 | <a onclick="return confirm('启用直播间?')" href="/live/roomoperate/<?=$model->id?>?type=enable" class="btn btn-success btn-metro">启用直播</a> | 123 | <a onclick="return confirm('启用直播间?')" href="/live/roomoperate/<?=$model->id?>?type=enable" class="btn btn-success btn-metro">启用直播</a> |
75 | <a onclick="return confirm('删除直播间?')" href="/live/roomoperate/<?=$model->id?>?type=del" class="btn btn-danger btn-metro">删除</a> | 124 | <a onclick="return confirm('删除直播间?')" href="/live/roomoperate/<?=$model->id?>?type=del" class="btn btn-danger btn-metro">删除</a> |
76 | <?php else:?> | 125 | <?php else:?> |
@@ -5,48 +5,18 @@ use backend\widgets\LinkPager; | @@ -5,48 +5,18 @@ use backend\widgets\LinkPager; | ||
5 | 5 | ||
6 | $this->title = $this->params['main_title'].'-'.$this->params['sub_title']; | 6 | $this->title = $this->params['main_title'].'-'.$this->params['sub_title']; |
7 | ?> | 7 | ?> |
8 | -<?php | ||
9 | -$this->registerJsFile('/js/php.js',array('postion'=>View::POS_END)); | ||
10 | -?> | 8 | + |
9 | +<?php echo $this->render('@app/views/layouts/websocket');?> | ||
10 | + | ||
11 | <script type="text/javascript"> | 11 | <script type="text/javascript"> |
12 | <?php $this->beginBlock('javascript');?> | 12 | <?php $this->beginBlock('javascript');?> |
13 | -var socket_arr = '<?=$wsAddr?>'.split(":"); | ||
14 | -var wsAddr = socket_arr[0]; | ||
15 | -var port = socket_arr[1]; | ||
16 | var i = 0; | 13 | var i = 0; |
17 | var room_id = <?=$room->room_id?>; | 14 | var room_id = <?=$room->room_id?>; |
18 | var interval_handle; | 15 | var interval_handle; |
19 | var default_interval = 3; | 16 | var default_interval = 3; |
20 | -/** | ||
21 | - * websocket推送指令 | ||
22 | - * @returns {undefined} | ||
23 | - */ | ||
24 | -function callWebsocket(callback){ | ||
25 | - if (!window.ws || window.ws.readyState === undefined || window.ws.readyState != 1) { | ||
26 | - websocket(wsAddr,port,'/',function(){ | ||
27 | - consoleLog('连接服务器'); | ||
28 | - | ||
29 | - callback(); | ||
30 | - },function(evt){ | ||
31 | - consoleLog('收到消息: '+evt.data); | ||
32 | - data = JSON.parse(evt.data); | ||
33 | - if (data.status == 1){ | ||
34 | - consoleLog('请求推送成功'); | ||
35 | - }else{ | ||
36 | - consoleLog('请求推送失败'); | ||
37 | - } | ||
38 | - },function(){ | ||
39 | - consoleLog('连接已关闭'); | ||
40 | - },function(){ | ||
41 | - consoleLog('有错误出现'); | ||
42 | - }); | ||
43 | - }else{ | ||
44 | - callback(); | ||
45 | - } | ||
46 | -} | 17 | + |
47 | /** | 18 | /** |
48 | * 推送商品列表刷新指令 | 19 | * 推送商品列表刷新指令 |
49 | - * @param {type} product_id | ||
50 | * @returns {undefined} | 20 | * @returns {undefined} |
51 | */ | 21 | */ |
52 | function refreshProduct(){ | 22 | function refreshProduct(){ |
@@ -121,31 +91,6 @@ function getPushlog(interval){ | @@ -121,31 +91,6 @@ function getPushlog(interval){ | ||
121 | $('#pushlog').text('开始监听推送记录').attr('onclick','getPushlog('+default_interval+')'); | 91 | $('#pushlog').text('开始监听推送记录').attr('onclick','getPushlog('+default_interval+')'); |
122 | } | 92 | } |
123 | } | 93 | } |
124 | -/** | ||
125 | - * 页面打印发送记录 | ||
126 | - * @param {type} msg | ||
127 | - * @returns {undefined} | ||
128 | - */ | ||
129 | -function consoleLog(msg){ | ||
130 | - if (msg){ | ||
131 | - console.log(msg); | ||
132 | - } | ||
133 | - $('#runtime').append(' '+msg+'<br />'); | ||
134 | -} | ||
135 | -/** | ||
136 | - * 密钥生成 | ||
137 | - * @param {type} package | ||
138 | - * @returns {unresolved} | ||
139 | - */ | ||
140 | -function makeSign(package){ | ||
141 | - package['private_key'] = 'a85bb0674e08986c6b115d5e3a4884fa'; | ||
142 | - package = ksort(package); | ||
143 | - packageList = []; | ||
144 | - for (key in package){ | ||
145 | - packageList.push(trim(key + '=' + package[key])); | ||
146 | - } | ||
147 | - return strtolower(md5(implode('&', packageList))); | ||
148 | -} | ||
149 | <?php | 94 | <?php |
150 | $this->endBlock(); | 95 | $this->endBlock(); |
151 | $this->registerJs($this->blocks['javascript'],View::POS_END) | 96 | $this->registerJs($this->blocks['javascript'],View::POS_END) |
@@ -214,6 +159,6 @@ $this->registerJs($this->blocks['javascript'],View::POS_END) | @@ -214,6 +159,6 @@ $this->registerJs($this->blocks['javascript'],View::POS_END) | ||
214 | </div> | 159 | </div> |
215 | 160 | ||
216 | <div class="form-group"> | 161 | <div class="form-group"> |
217 | - <div class="col-sm-6" id="runtime"> | 162 | + <div class="col-sm-6" id="wsRuntime"> |
218 | </div> | 163 | </div> |
219 | </div><!-- form-group --> | 164 | </div><!-- form-group --> |
-
Please register or login to post a comment