From 74460e7c9553be8670946062a8c0372ee6fd6d33 Mon Sep 17 00:00:00 2001 From: Lawrence <1934378145@qq.com> Date: Thu, 21 Jan 2021 19:57:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8F=E8=A7=88=E5=99=A8ID?= =?UTF-8?q?=EF=BC=8C=E7=A1=AE=E4=BF=9DSSE=E5=8F=AF=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E5=88=B0=E4=B8=8D=E5=90=8C=E7=9A=84=E5=89=8D?= =?UTF-8?q?=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/alarm/AlarmEventListener.java | 39 +++++++++------ .../SseController.java} | 11 +++-- web_src/package-lock.json | 49 ++++++++++++++----- web_src/package.json | 2 + web_src/src/components/UiHeader.vue | 3 +- web_src/src/main.js | 22 ++++++++- 6 files changed, 92 insertions(+), 34 deletions(-) rename src/main/java/com/genersoft/iot/vmp/vmanager/{SEEController/SEEController.java => SseController/SseController.java} (67%) diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/event/alarm/AlarmEventListener.java b/src/main/java/com/genersoft/iot/vmp/gb28181/event/alarm/AlarmEventListener.java index 1b2f6723..2b563269 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/event/alarm/AlarmEventListener.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/event/alarm/AlarmEventListener.java @@ -4,6 +4,10 @@ import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import java.io.IOException; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,10 +22,10 @@ public class AlarmEventListener implements ApplicationListener { private final static Logger logger = LoggerFactory.getLogger(AlarmEventListener.class); - private static SseEmitter emitter = new SseEmitter(); + private static Map sseEmitters = new Hashtable<>(); - public void addSseEmitters(SseEmitter sseEmitter) { - emitter = sseEmitter; + public void addSseEmitters(String browserId, SseEmitter sseEmitter) { + sseEmitters.put(browserId, sseEmitter); } @Override @@ -30,18 +34,25 @@ public class AlarmEventListener implements ApplicationListener { logger.debug("设备报警事件触发,deviceId:" + event.getAlarmInfo().getDeviceId() + ", " + event.getAlarmInfo().getAlarmDescription()); } - try { - String msg = "设备编码: " + event.getAlarmInfo().getDeviceId() + "" - + "
报警描述: " + event.getAlarmInfo().getAlarmDescription() + "" - + "
报警时间: " + event.getAlarmInfo().getAlarmTime() + "" - + "
定位经度: " + event.getAlarmInfo().getLongitude() + "" - + "
定位纬度: " + event.getAlarmInfo().getLatitude() + ""; - emitter.send(msg); - } catch (IOException e) { - if (logger.isDebugEnabled()) { - logger.debug("SSE 通道已关闭"); + String msg = "设备编码: " + event.getAlarmInfo().getDeviceId() + "" + + "
报警描述: " + event.getAlarmInfo().getAlarmDescription() + "" + + "
报警时间: " + event.getAlarmInfo().getAlarmTime() + "" + + "
报警位置: " + event.getAlarmInfo().getLongitude() + "" + + ", " + event.getAlarmInfo().getLatitude() + ""; + + for (Iterator> it = sseEmitters.entrySet().iterator(); it.hasNext();) { + Map.Entry emitter = it.next(); + logger.info("推送到SSE连接,浏览器ID: " + emitter.getKey()); + try { + emitter.getValue().send(msg); + } catch (IOException | IllegalStateException e) { + if (logger.isDebugEnabled()) { + logger.debug("SSE连接已关闭"); + } + // 移除已关闭的连接 + it.remove(); + // e.printStackTrace(); } - // e.printStackTrace(); } } } diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/SEEController/SEEController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/SseController/SseController.java similarity index 67% rename from src/main/java/com/genersoft/iot/vmp/vmanager/SEEController/SEEController.java rename to src/main/java/com/genersoft/iot/vmp/vmanager/SseController/SseController.java index 689b967b..01af4a0d 100644 --- a/src/main/java/com/genersoft/iot/vmp/vmanager/SEEController/SEEController.java +++ b/src/main/java/com/genersoft/iot/vmp/vmanager/SseController/SseController.java @@ -1,9 +1,10 @@ -package com.genersoft.iot.vmp.vmanager.SEEController; +package com.genersoft.iot.vmp.vmanager.SseController; import com.genersoft.iot.vmp.gb28181.event.alarm.AlarmEventListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; /** @@ -14,16 +15,16 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; @Controller @RequestMapping("/api") -public class SEEController { +public class SseController { @Autowired AlarmEventListener alarmEventListener; //设置响应 @RequestMapping("/emit") - public SseEmitter emit() { - SseEmitter sseEmitter = new SseEmitter(0L); + public SseEmitter emit(@RequestParam String browserId) { + final SseEmitter sseEmitter = new SseEmitter(0L); try { - alarmEventListener.addSseEmitters(sseEmitter); + alarmEventListener.addSseEmitters(browserId, sseEmitter); }catch (Exception e){ sseEmitter.completeWithError(e); } diff --git a/web_src/package-lock.json b/web_src/package-lock.json index 334f488a..fb6278de 100644 --- a/web_src/package-lock.json +++ b/web_src/package-lock.json @@ -99,7 +99,6 @@ "version": "3.2.1", "resolved": "https://registry.npm.taobao.org/ansi-styles/download/ansi-styles-3.2.1.tgz", "integrity": "sha1-QfuyAkPlCxK+DwS43tvwdSDOhB0=", - "dev": true, "requires": { "color-convert": "^1.9.0" } @@ -1645,7 +1644,6 @@ "version": "2.4.2", "resolved": "https://registry.npm.taobao.org/chalk/download/chalk-2.4.2.tgz", "integrity": "sha1-zUJUFnelQzPPVBpJEIwUMrRMlCQ=", - "dev": true, "requires": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", @@ -1847,7 +1845,6 @@ "version": "1.9.3", "resolved": "https://registry.npm.taobao.org/color-convert/download/color-convert-1.9.3.tgz", "integrity": "sha1-u3GFBpDh8TZWfeYp0tVHHe2kweg=", - "dev": true, "requires": { "color-name": "1.1.3" } @@ -1855,8 +1852,7 @@ "color-name": { "version": "1.1.3", "resolved": "https://registry.npm.taobao.org/color-name/download/color-name-1.1.3.tgz", - "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", - "dev": true + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, "color-string": { "version": "0.3.0", @@ -3713,8 +3709,7 @@ "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npm.taobao.org/escape-string-regexp/download/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", - "dev": true + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" }, "escope": { "version": "3.6.0", @@ -4150,6 +4145,11 @@ "locate-path": "^2.0.0" } }, + "fingerprintjs2": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/fingerprintjs2/-/fingerprintjs2-2.1.2.tgz", + "integrity": "sha512-ZPsLgjziFRbUb5tXWpEMtWp4XFnzSah8SiNfl3aoURDZ+2zi2tuIOYUULqDBV+Cb6paN+raWT+Q2qpOaCbX/Yw==" + }, "flatten": { "version": "1.0.3", "resolved": "https://registry.npm.taobao.org/flatten/download/flatten-1.0.3.tgz", @@ -4403,8 +4403,7 @@ "has-flag": { "version": "3.0.0", "resolved": "https://registry.npm.taobao.org/has-flag/download/has-flag-3.0.0.tgz", - "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", - "dev": true + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" }, "has-symbols": { "version": "1.0.1", @@ -8437,6 +8436,34 @@ } } }, + "postcss-pxtorem": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/postcss-pxtorem/-/postcss-pxtorem-5.1.1.tgz", + "integrity": "sha512-uvgIujL/pn0GbZ+rczESD2orHsbXrrCqi+q9wJO8PCk3ZGCoVVtu5hZTbtk+tbZHZP5UkTfCvqOrTZs9Ncqfsg==", + "requires": { + "postcss": "^7.0.27" + }, + "dependencies": { + "postcss": { + "version": "7.0.35", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-7.0.35.tgz", + "integrity": "sha512-3QT8bBJeX/S5zKTTjTCIjRF3If4avAT6kqxcASlTWEtAFCb9NH0OUxNDfgZSWdP5fJnBYCMEWkIFfWeugjzYMg==", + "requires": { + "chalk": "^2.4.2", + "source-map": "^0.6.1", + "supports-color": "^6.1.0" + } + }, + "supports-color": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-6.1.0.tgz", + "integrity": "sha512-qe1jfm1Mg7Nq/NSh6XE24gPXROEVsWHxC1LIx//XNlD9iw7YZQGjZNjYN7xGaEG6iKdA8EtNFW6R0gjnVXp+wQ==", + "requires": { + "has-flag": "^3.0.0" + } + } + } + }, "postcss-reduce-idents": { "version": "2.4.0", "resolved": "https://registry.npm.taobao.org/postcss-reduce-idents/download/postcss-reduce-idents-2.4.0.tgz?cache=0&sync_timestamp=1599672339373&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fpostcss-reduce-idents%2Fdownload%2Fpostcss-reduce-idents-2.4.0.tgz", @@ -9893,8 +9920,7 @@ "source-map": { "version": "0.6.1", "resolved": "https://registry.npm.taobao.org/source-map/download/source-map-0.6.1.tgz", - "integrity": "sha1-dHIq8y6WFOnCh6jQu95IteLxomM=", - "dev": true + "integrity": "sha1-dHIq8y6WFOnCh6jQu95IteLxomM=" }, "source-map-resolve": { "version": "0.5.3", @@ -10290,7 +10316,6 @@ "version": "5.5.0", "resolved": "https://registry.npm.taobao.org/supports-color/download/supports-color-5.5.0.tgz?cache=0&sync_timestamp=1598611719015&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fsupports-color%2Fdownload%2Fsupports-color-5.5.0.tgz", "integrity": "sha1-4uaaRKyHcveKHsCzW2id9lMO/I8=", - "dev": true, "requires": { "has-flag": "^3.0.0" } diff --git a/web_src/package.json b/web_src/package.json index c949393a..d99d7bcd 100644 --- a/web_src/package.json +++ b/web_src/package.json @@ -15,7 +15,9 @@ "core-js": "^2.6.5", "echarts": "^4.7.0", "element-ui": "2.10.1", + "fingerprintjs2": "^2.1.2", "moment": "^2.29.1", + "postcss-pxtorem": "^5.1.1", "vue": "^2.6.11", "vue-clipboard2": "^0.3.1", "vue-cookies": "^1.7.4", diff --git a/web_src/src/components/UiHeader.vue b/web_src/src/components/UiHeader.vue index f16beba8..282a89d1 100644 --- a/web_src/src/components/UiHeader.vue +++ b/web_src/src/components/UiHeader.vue @@ -34,7 +34,8 @@ export default { sseControl() { let that = this; if (this.alarmNotify) { - this.sseSource = new EventSource('/api/emit'); + console.log("申请SSE推送API调用,浏览器ID: " + this.$browserId); + this.sseSource = new EventSource('/api/emit?browserId=' + this.$browserId); this.sseSource.addEventListener('message', function(evt) { that.$notify({ title: '收到报警信息', diff --git a/web_src/src/main.js b/web_src/src/main.js index ae2baeef..03bb4a48 100644 --- a/web_src/src/main.js +++ b/web_src/src/main.js @@ -8,10 +8,28 @@ import axios from 'axios'; import VueCookies from 'vue-cookies'; import echarts from 'echarts'; -import VueClipboard from 'vue-clipboard2' +import VueClipboard from 'vue-clipboard2'; import { Notification } from 'element-ui'; +import Fingerprint2 from 'fingerprintjs2'; -Vue.use(VueClipboard) +// ΨһID +Fingerprint2.get(function(components) { + const values = components.map(function(component,index) { + if (index === 0) { //΢UAwifi4G滻ɿ,ȻлIDһ + return component.value.replace(/\bNetType\/\w+\b/, ''); + } + return component.value; + }) + //console.log(values) //ʹõϢnpm + // id + let port = window.location.port; + console.log(port); + const fingerPrint = Fingerprint2.x64hash128(values.join(port), 31) + Vue.prototype.$browserId = fingerPrint; + console.log("Ψһʶ룺" + fingerPrint); +}); + +Vue.use(VueClipboard); Vue.use(ElementUI); Vue.use(VueCookies); Vue.prototype.$axios = axios;