Commit 3082cb42 authored by 冯超鹏's avatar 冯超鹏

提交长连接设备监测模块

parent 606f7980
...@@ -67,13 +67,6 @@ class swoole extends Command ...@@ -67,13 +67,6 @@ class swoole extends Command
)); ));
$this->ws->on('open', function ($ws, $request) { $this->ws->on('open', function ($ws, $request) {
// //链接成功 // //链接成功
// $data = [
// 'code' => 200,
// 'token' => md5($this->token),
// 'time' => date("Y-m-d H:i:s", time()),
// 'msg' => '连接成功'
// ];
// $this->ws->push(1, json_encode($data), 1);
$userdata = new SwooleCommandController(); $userdata = new SwooleCommandController();
$this->ws->push($request->fd,$userdata->userslist(),1); $this->ws->push($request->fd,$userdata->userslist(),1);
swoole_timer_tick(6000, function($timerId) use ($ws, $request) { swoole_timer_tick(6000, function($timerId) use ($ws, $request) {
...@@ -85,32 +78,9 @@ class swoole extends Command ...@@ -85,32 +78,9 @@ class swoole extends Command
//监听WebSocket消息事件 //监听WebSocket消息事件
$this->ws->on('message', function ($ws, $frame) { $this->ws->on('message', function ($ws, $frame) {
// $data = json_decode($frame->data, true);
// //监测是否是有效链接
// if($this->ws->isEstablished($frame->fd)){
// $ws->bind($frame->fd, $data['id']);
// $userdata = new SwooleCommandController();
// $this->ws->push($frame->fd,$userdata->userslist(),1);
// }else{
// Log::channel('single')->info('不是有效连接'. ','.'id=>'. $frame->fd);
// $this->ws->push($frame->fd, '不是有效连接', 1);
// $this->ws->disconnect($frame->fd, 105, '已断开连接,不是有效连接');
// }
//// $token = (string)$data['token'];
//// if ($token != md5($this->token)) {
//// Log::channel('single')->info('token错误'. ',' .'token=>'. $token . ',' . 'id=>' . $frame->fd);
//// $this->ws->push($frame->fd, 'token错误', 1);
//// $this->ws->disconnect($frame->fd, 105, '已断开连接,token错误');
//// } else {
//////
//// }
}); });
$this->ws->on('request', function ($request, $response) { $this->ws->on('request', function ($request, $response,$from_id,$fd) {
if ($request->post['token'] == "123") {
echo "123213";
$clients = $this->ws->getClientList();
}
}); });
//监听WebSocket连接关闭事件 //监听WebSocket连接关闭事件
$this->ws->on('close', function ($ws, $fd) { $this->ws->on('close', function ($ws, $fd) {
......
...@@ -24,5 +24,36 @@ class SwooleCommandController extends Controller ...@@ -24,5 +24,36 @@ class SwooleCommandController extends Controller
$countdata = ['usercount' => $usercount, 'userdata' => $count]; $countdata = ['usercount' => $usercount, 'userdata' => $count];
return $this->jsonSuccessData($countdata); return $this->jsonSuccessData($countdata);
} }
public function chemicals(){
$databadevice = DB::table('device');
$devicelist = $databadevice
->whereIn('dtype', [1, 5, 10])
->leftjoin('device_type as dy', "device.dtype", '=', 'dy.tid')
->leftjoin('gas as g', "device.status", '=', 'g.id')
->leftjoin('status as p', "device.devicepolice", '=', 'p.id')
->select('dy.tname', 'g.gas', 'device.*', 'p.status_name')
->orderBy('device.id', 'desc')
->get()->toArray();
$count = $databadevice
->whereIn('dtype', [1, 5, 10])
->count();
return $this->jsonSuccessData(['devicelist' => $devicelist, 'count' => $count]);
}
public function fire(){
$databadevice = DB::table('device');
$devicelist = $databadevice
->whereIn('dtype', [2, 4, 6, 7, 8, 9, 11])
->leftjoin('device_type as dy', "device.dtype", '=', 'dy.tid')
->leftjoin('gas as g', "device.status", '=', 'g.id')
->leftjoin('status as p', "device.devicepolice", '=', 'p.id')
->select('dy.tname', 'g.gas', 'device.*', 'p.status_name')
->orderBy('device.id', 'desc')
->get()->toArray();
$count = $databadevice
->whereIn('dtype', [2, 4, 6, 7, 8, 9, 11])
->count();
return $this->jsonSuccessData(['devicelist' => $devicelist, 'count' => $count]);
}
} }
\ No newline at end of file
...@@ -68,7 +68,6 @@ class Controller extends BaseController ...@@ -68,7 +68,6 @@ class Controller extends BaseController
], ],
]; ];
} }
/* /*
* 验证当前登入用户 * 验证当前登入用户
* */ * */
......
const baseURL = 'ws://127.0.0.1:9502'; const baseURL = 'ws://127.0.0.1:9502';
const chemicalsUrl = 'ws://127.0.0.1:9507';
const fireUrl = 'ws://127.0.0.1:9508';
const mapkey = '2719fe261fee06a08dcb4980990879da'; const mapkey = '2719fe261fee06a08dcb4980990879da';
const mapurl = 'https://webapi.amap.com/maps?v=1.4.15&key='; const mapurl = 'https://webapi.amap.com/maps?v=1.4.15&key=';
export default { export default {
baseURL, baseURL,
mapkey, mapkey,
mapurl, mapurl,
chemicalsUrl,
fireUrl,
}; };
...@@ -72,6 +72,7 @@ ...@@ -72,6 +72,7 @@
<script> <script>
import { control } from '@/api/device'; import { control } from '@/api/device';
import Pagination from '@/components/Pagination'; // 分页 import Pagination from '@/components/Pagination'; // 分页
import websocketurl from '@/api/configurl';
import clip from '@/utils/clipboard'; import clip from '@/utils/clipboard';
export default { export default {
name: 'Devicetype', name: 'Devicetype',
...@@ -90,6 +91,7 @@ export default { ...@@ -90,6 +91,7 @@ export default {
}, },
created() { created() {
this.devicelist(); // 设备状态 this.devicelist(); // 设备状态
this.initWebSocket();
}, },
methods: { methods: {
...@@ -111,6 +113,41 @@ export default { ...@@ -111,6 +113,41 @@ export default {
console.log(err); console.log(err);
}); });
}, },
initWebSocket(){ // 初始化weosocket
const wsuri = websocketurl.chemicalsUrl;
this.websock = new WebSocket(wsuri);
this.websock.onmessage = this.websocketonmessage;
this.websock.onopen = this.websocketonopen;
this.websock.onerror = this.websocketonerror;
this.websock.onclose = this.websocketclose;
},
websocketonopen(){ // 连接建立之后执行send方法发送数据
console.log('连接成功');
},
websocketonerror(){ // 连接建立失败重连
this.$notify({
title: '警告',
message: '建立链接失败,正在重连',
type: 'warning',
});
this.initWebSocket();
},
websocketonmessage(e){ // 数据接收
const redata = JSON.parse(e.data);
this.device = redata.data.devicelist;
this.total = redata.data.count;
},
websocketsend(){ // 数据发送
this.websock.send(JSON.stringify(this.type));
},
websocketclose(e){ // 关闭
console.log('断开连接', e);
this.$notify({
title: '警告',
message: '长连接已断开',
type: 'warning',
});
},
handleClick(tab) { handleClick(tab) {
this.type = tab.$attrs.tid; this.type = tab.$attrs.tid;
this.devicelist(); this.devicelist();
......
...@@ -72,6 +72,7 @@ ...@@ -72,6 +72,7 @@
<script> <script>
import { control } from '@/api/device'; import { control } from '@/api/device';
import Pagination from '@/components/Pagination'; // 分页 import Pagination from '@/components/Pagination'; // 分页
import websocketurl from '@/api/configurl';
import clip from '@/utils/clipboard'; import clip from '@/utils/clipboard';
export default { export default {
name: 'Devicetype', name: 'Devicetype',
...@@ -90,6 +91,7 @@ export default { ...@@ -90,6 +91,7 @@ export default {
}, },
created() { created() {
this.devicelist(); // 设备状态 this.devicelist(); // 设备状态
this.initWebSocket();
}, },
methods: { methods: {
...@@ -111,6 +113,41 @@ export default { ...@@ -111,6 +113,41 @@ export default {
console.log(err); console.log(err);
}); });
}, },
initWebSocket(){ // 初始化weosocket
const wsuri = websocketurl.fireUrl;
this.websock = new WebSocket(wsuri);
this.websock.onmessage = this.websocketonmessage;
this.websock.onopen = this.websocketonopen;
this.websock.onerror = this.websocketonerror;
this.websock.onclose = this.websocketclose;
},
websocketonopen(){ // 连接建立之后执行send方法发送数据
console.log('连接成功');
},
websocketonerror(){ // 连接建立失败重连
this.$notify({
title: '警告',
message: '建立链接失败,正在重连',
type: 'warning',
});
this.initWebSocket();
},
websocketonmessage(e){ // 数据接收
const redata = JSON.parse(e.data);
this.device = redata.data.devicelist;
this.total = redata.data.count;
},
websocketsend(){ // 数据发送
this.websock.send(JSON.stringify(this.type));
},
websocketclose(e){ // 关闭
console.log('断开连接', e);
this.$notify({
title: '警告',
message: '长连接已断开',
type: 'warning',
});
},
handleClick(tab) { handleClick(tab) {
this.type = tab.$attrs.tid; this.type = tab.$attrs.tid;
this.devicelist(); this.devicelist();
......
...@@ -136,7 +136,6 @@ export default { ...@@ -136,7 +136,6 @@ export default {
}, },
websocketonmessage(e){ // 数据接收 websocketonmessage(e){ // 数据接收
const redata = JSON.parse(e.data); const redata = JSON.parse(e.data);
this.type.token = redata.token;
this.countdata = redata.data.userdata; this.countdata = redata.data.userdata;
this.devicenum = redata.data.usercount; this.devicenum = redata.data.usercount;
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment