diff --git a/config/dev.env.js b/config/dev.env.js index 5cad7f4..53f67fb 100644 --- a/config/dev.env.js +++ b/config/dev.env.js @@ -6,5 +6,7 @@ // 将两个配置对象合并,最终结果是 NODE_ENV:‘"development"' module.exports = merge(prodEnv, { NODE_ENV: '"development"', - BASE_API: '"http://111.198.10.15:12210/pan-tilt/"' + // BASE_API: '"http://111.198.10.15:12210/pan-tilt/"' + BASE_API: '"http://127.0.0.1:8080/pan-tilt/"', + BASE_WS: '"ws://127.0.0.1:8080/pan-tilt/websocket/2"' }) diff --git a/config/dev.env.js b/config/dev.env.js index 5cad7f4..53f67fb 100644 --- a/config/dev.env.js +++ b/config/dev.env.js @@ -6,5 +6,7 @@ // 将两个配置对象合并,最终结果是 NODE_ENV:‘"development"' module.exports = merge(prodEnv, { NODE_ENV: '"development"', - BASE_API: '"http://111.198.10.15:12210/pan-tilt/"' + // BASE_API: '"http://111.198.10.15:12210/pan-tilt/"' + BASE_API: '"http://127.0.0.1:8080/pan-tilt/"', + BASE_WS: '"ws://127.0.0.1:8080/pan-tilt/websocket/2"' }) diff --git a/config/prod.env.js b/config/prod.env.js index 6a9dcf4..e180f6e 100644 --- a/config/prod.env.js +++ b/config/prod.env.js @@ -1,5 +1,8 @@ 'use strict' module.exports = { NODE_ENV: '"production"', - BASE_API: '"http://111.198.10.15:12210/pan-tilt/"' + BASE_API: '"http://111.198.10.15:12210/pan-tilt/"', + BASE_WS: '"ws:///111.198.10.15:12210/pan-tilt/websocket/2"' + // BASE_API: '"http://127.0.0.1:8080/pan-tilt/"', + // BASE_WS: '"ws://192.168.10.200:8080/pan-tilt/websocket/2"' } diff --git a/config/dev.env.js b/config/dev.env.js index 5cad7f4..53f67fb 100644 --- a/config/dev.env.js +++ b/config/dev.env.js @@ -6,5 +6,7 @@ // 将两个配置对象合并,最终结果是 NODE_ENV:‘"development"' module.exports = merge(prodEnv, { NODE_ENV: '"development"', - BASE_API: '"http://111.198.10.15:12210/pan-tilt/"' + // BASE_API: '"http://111.198.10.15:12210/pan-tilt/"' + BASE_API: '"http://127.0.0.1:8080/pan-tilt/"', + BASE_WS: '"ws://127.0.0.1:8080/pan-tilt/websocket/2"' }) diff --git a/config/prod.env.js b/config/prod.env.js index 6a9dcf4..e180f6e 100644 --- a/config/prod.env.js +++ b/config/prod.env.js @@ -1,5 +1,8 @@ 'use strict' module.exports = { NODE_ENV: '"production"', - BASE_API: '"http://111.198.10.15:12210/pan-tilt/"' + BASE_API: '"http://111.198.10.15:12210/pan-tilt/"', + BASE_WS: '"ws:///111.198.10.15:12210/pan-tilt/websocket/2"' + // BASE_API: '"http://127.0.0.1:8080/pan-tilt/"', + // BASE_WS: '"ws://192.168.10.200:8080/pan-tilt/websocket/2"' } diff --git a/src/layout/components/AppHeader.vue b/src/layout/components/AppHeader.vue index 571db35..577a45b 100644 --- a/src/layout/components/AppHeader.vue +++ b/src/layout/components/AppHeader.vue @@ -29,15 +29,17 @@ import { mapGetters } from 'vuex' import ResetPwd from '@/views/system/user/resetPwd' import { getProject } from '@/utils/baseConfig' +import { Notification } from 'element-ui' export default { name: 'AppHeader', components: { ResetPwd }, data() { return { + wsPath: process.env.BASE_WS, title: getProject().title, name: this.$store.getters.name, - showSetPwd: false, + showSetPwd: false // headerUrl: require('../../../assets/global_images/header.png')// 头部图片 } }, @@ -47,7 +49,66 @@ 'avatar' ]) }, + mounted() { + this.$nextTick(() => { + this.initWebSocket() + }) + }, methods: { + initWebSocket() { + const that = this + if (typeof (WebSocket) === 'undefined') { + Notification({ + title: '提示', + message: '当前浏览器无法接收实时报警信息,请使用谷歌浏览器或360浏览器极速模式!', + type: 'warning', + duration: 0 + }) + } else { + const socketUrl = this.wsPath + console.log(socketUrl, '*********************') + that.socket = new WebSocket(socketUrl) + // 监听socket打开 + that.socket.onopen = function() { + console.log('浏览器WebSocket已打开') + } + // 监听socket消息接收 + that.socket.onmessage = function(msg) { + // 转换为json对象 + const data = JSON.parse(msg.data) + if (data.type === 'gasAlarm') { + const message = data.monitorName + '设备报警甲烷值:' + data.concentration + ',报警位置:' + data.alarmDirection + ',' + data.alarmPitch + Notification({ + title: '甲烷超标报警', + // 这里也可以把返回信息加入到message中显示 + message: message, + type: 'warning' + }) + } else if (data.type === 'strengthAlarm') { + const message = data.monitorName + '设备报警遮挡' + Notification({ + title: '遮挡报警', + // 这里也可以把返回信息加入到message中显示 + message: message, + type: 'warning' + }) + } + } + } + // 监听socket错误 + that.socket.onerror = function() { + Notification({ + title: '服务器错误', + message: '无法接收实时报警信息,请检查服务器后重新刷新页面', + type: 'error', + duration: 0 + }) + } + // 监听socket关闭 + that.socket.onclose = function() { + console.log('WebSocket已关闭') + } + }, toggleSideBar() { this.$store.dispatch('ToggleSideBar') }, diff --git a/config/dev.env.js b/config/dev.env.js index 5cad7f4..53f67fb 100644 --- a/config/dev.env.js +++ b/config/dev.env.js @@ -6,5 +6,7 @@ // 将两个配置对象合并,最终结果是 NODE_ENV:‘"development"' module.exports = merge(prodEnv, { NODE_ENV: '"development"', - BASE_API: '"http://111.198.10.15:12210/pan-tilt/"' + // BASE_API: '"http://111.198.10.15:12210/pan-tilt/"' + BASE_API: '"http://127.0.0.1:8080/pan-tilt/"', + BASE_WS: '"ws://127.0.0.1:8080/pan-tilt/websocket/2"' }) diff --git a/config/prod.env.js b/config/prod.env.js index 6a9dcf4..e180f6e 100644 --- a/config/prod.env.js +++ b/config/prod.env.js @@ -1,5 +1,8 @@ 'use strict' module.exports = { NODE_ENV: '"production"', - BASE_API: '"http://111.198.10.15:12210/pan-tilt/"' + BASE_API: '"http://111.198.10.15:12210/pan-tilt/"', + BASE_WS: '"ws:///111.198.10.15:12210/pan-tilt/websocket/2"' + // BASE_API: '"http://127.0.0.1:8080/pan-tilt/"', + // BASE_WS: '"ws://192.168.10.200:8080/pan-tilt/websocket/2"' } diff --git a/src/layout/components/AppHeader.vue b/src/layout/components/AppHeader.vue index 571db35..577a45b 100644 --- a/src/layout/components/AppHeader.vue +++ b/src/layout/components/AppHeader.vue @@ -29,15 +29,17 @@ import { mapGetters } from 'vuex' import ResetPwd from '@/views/system/user/resetPwd' import { getProject } from '@/utils/baseConfig' +import { Notification } from 'element-ui' export default { name: 'AppHeader', components: { ResetPwd }, data() { return { + wsPath: process.env.BASE_WS, title: getProject().title, name: this.$store.getters.name, - showSetPwd: false, + showSetPwd: false // headerUrl: require('../../../assets/global_images/header.png')// 头部图片 } }, @@ -47,7 +49,66 @@ 'avatar' ]) }, + mounted() { + this.$nextTick(() => { + this.initWebSocket() + }) + }, methods: { + initWebSocket() { + const that = this + if (typeof (WebSocket) === 'undefined') { + Notification({ + title: '提示', + message: '当前浏览器无法接收实时报警信息,请使用谷歌浏览器或360浏览器极速模式!', + type: 'warning', + duration: 0 + }) + } else { + const socketUrl = this.wsPath + console.log(socketUrl, '*********************') + that.socket = new WebSocket(socketUrl) + // 监听socket打开 + that.socket.onopen = function() { + console.log('浏览器WebSocket已打开') + } + // 监听socket消息接收 + that.socket.onmessage = function(msg) { + // 转换为json对象 + const data = JSON.parse(msg.data) + if (data.type === 'gasAlarm') { + const message = data.monitorName + '设备报警甲烷值:' + data.concentration + ',报警位置:' + data.alarmDirection + ',' + data.alarmPitch + Notification({ + title: '甲烷超标报警', + // 这里也可以把返回信息加入到message中显示 + message: message, + type: 'warning' + }) + } else if (data.type === 'strengthAlarm') { + const message = data.monitorName + '设备报警遮挡' + Notification({ + title: '遮挡报警', + // 这里也可以把返回信息加入到message中显示 + message: message, + type: 'warning' + }) + } + } + } + // 监听socket错误 + that.socket.onerror = function() { + Notification({ + title: '服务器错误', + message: '无法接收实时报警信息,请检查服务器后重新刷新页面', + type: 'error', + duration: 0 + }) + } + // 监听socket关闭 + that.socket.onclose = function() { + console.log('WebSocket已关闭') + } + }, toggleSideBar() { this.$store.dispatch('ToggleSideBar') }, diff --git a/src/views/alarmManage/alarmDetail.vue b/src/views/alarmManage/alarmDetail.vue index 57d601c..2911e71 100644 --- a/src/views/alarmManage/alarmDetail.vue +++ b/src/views/alarmManage/alarmDetail.vue @@ -1,5 +1,5 @@