Browse Source

Merge pull request #46 from lawrencehj/wvp-28181-2.0

修正SSE不能分别发送到同时浏览的前端的问题等
pull/58/head
648540858 4 years ago
committed by GitHub
parent
commit
9d19f6b9d6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      src/main/java/com/genersoft/iot/vmp/gb28181/event/alarm/AlarmEventListener.java
  2. 11
      src/main/java/com/genersoft/iot/vmp/vmanager/SseController/SseController.java
  3. 16
      src/main/resources/application-dev.yml
  4. 6
      web_src/.postcssrc.js
  5. 49
      web_src/package-lock.json
  6. 2
      web_src/package.json
  7. 3
      web_src/src/components/UiHeader.vue
  8. 22
      web_src/src/main.js

29
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.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.IOException; import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -18,10 +22,10 @@ public class AlarmEventListener implements ApplicationListener<AlarmEvent> {
private final static Logger logger = LoggerFactory.getLogger(AlarmEventListener.class); private final static Logger logger = LoggerFactory.getLogger(AlarmEventListener.class);
private static SseEmitter emitter = new SseEmitter(); private static Map<String, SseEmitter> sseEmitters = new Hashtable<>();
public void addSseEmitters(SseEmitter sseEmitter) { public void addSseEmitters(String browserId, SseEmitter sseEmitter) {
emitter = sseEmitter; sseEmitters.put(browserId, sseEmitter);
} }
@Override @Override
@ -30,18 +34,25 @@ public class AlarmEventListener implements ApplicationListener<AlarmEvent> {
logger.debug("设备报警事件触发,deviceId:" + event.getAlarmInfo().getDeviceId() + ", " logger.debug("设备报警事件触发,deviceId:" + event.getAlarmInfo().getDeviceId() + ", "
+ event.getAlarmInfo().getAlarmDescription()); + event.getAlarmInfo().getAlarmDescription());
} }
try {
String msg = "<strong>设备编码:</strong> <i>" + event.getAlarmInfo().getDeviceId() + "</i>" String msg = "<strong>设备编码:</strong> <i>" + event.getAlarmInfo().getDeviceId() + "</i>"
+ "<br><strong>报警描述:</strong> <i>" + event.getAlarmInfo().getAlarmDescription() + "</i>" + "<br><strong>报警描述:</strong> <i>" + event.getAlarmInfo().getAlarmDescription() + "</i>"
+ "<br><strong>报警时间:</strong> <i>" + event.getAlarmInfo().getAlarmTime() + "</i>" + "<br><strong>报警时间:</strong> <i>" + event.getAlarmInfo().getAlarmTime() + "</i>"
+ "<br><strong>定位经度:</strong> <i>" + event.getAlarmInfo().getLongitude() + "</i>" + "<br><strong>报警位置:</strong> <i>" + event.getAlarmInfo().getLongitude() + "</i>"
+ "<br><strong>定位纬度:</strong> <i>" + event.getAlarmInfo().getLatitude() + "</i>"; + ", <i>" + event.getAlarmInfo().getLatitude() + "</i>";
emitter.send(msg);
} catch (IOException e) { for (Iterator<Map.Entry<String, SseEmitter>> it = sseEmitters.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, SseEmitter> emitter = it.next();
logger.info("推送到SSE连接,浏览器ID: " + emitter.getKey());
try {
emitter.getValue().send(msg);
} catch (IOException | IllegalStateException e) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("SSE 通道已关闭"); logger.debug("SSE连接已关闭");
} }
// 移除已关闭的连接
it.remove();
// e.printStackTrace(); // e.printStackTrace();
} }
} }
} }
}

11
src/main/java/com/genersoft/iot/vmp/vmanager/SEEController/SEEController.java → 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 com.genersoft.iot.vmp.gb28181.event.alarm.AlarmEventListener;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
/** /**
@ -14,16 +15,16 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@Controller @Controller
@RequestMapping("/api") @RequestMapping("/api")
public class SEEController { public class SseController {
@Autowired @Autowired
AlarmEventListener alarmEventListener; AlarmEventListener alarmEventListener;
//设置响应 //设置响应
@RequestMapping("/emit") @RequestMapping("/emit")
public SseEmitter emit() { public SseEmitter emit(@RequestParam String browserId) {
SseEmitter sseEmitter = new SseEmitter(0L); final SseEmitter sseEmitter = new SseEmitter(0L);
try { try {
alarmEventListener.addSseEmitters(sseEmitter); alarmEventListener.addSseEmitters(browserId, sseEmitter);
}catch (Exception e){ }catch (Exception e){
sseEmitter.completeWithError(e); sseEmitter.completeWithError(e);
} }

16
src/main/resources/application-dev.yml

@ -35,18 +35,18 @@ server:
# 作为28181服务器的配置 # 作为28181服务器的配置
sip: sip:
# [必须修改] 本机的IP, 必须是网卡上的IP # [必须修改] 本机的IP, 必须是网卡上的IP
ip: 192.168.1.44 ip: 192.168.0.100
# [可选] 28181服务监听的端口 # [可选] 28181服务监听的端口
port: 5060 port: 5060
# 根据国标6.1.2中规定,domain宜采用ID统一编码的前十位编码。国标附录D中定义前8位为中心编码(由省级、市级、区级、基层编号组成,参照GB/T 2260-2007) # 根据国标6.1.2中规定,domain宜采用ID统一编码的前十位编码。国标附录D中定义前8位为中心编码(由省级、市级、区级、基层编号组成,参照GB/T 2260-2007)
# 后两位为行业编码,定义参照附录D.3 # 后两位为行业编码,定义参照附录D.3
# 3701020049标识山东济南历下区 信息行业接入 # 3701020049标识山东济南历下区 信息行业接入
# [可选] # [可选]
domain: 3402000000 domain: 4401020049
# [可选] # [可选]
id: 34020000002000000001 id: 44010200492000000001
# [可选] 默认设备认证密码,后续扩展使用设备单独密码 # [可选] 默认设备认证密码,后续扩展使用设备单独密码
password: 12345678 password: admin123
# 登陆的用户名密码 # 登陆的用户名密码
auth: auth:
@ -58,7 +58,7 @@ auth:
#zlm服务器配置 #zlm服务器配置
media: media:
# [必须修改] zlm服务器的内网IP # [必须修改] zlm服务器的内网IP
ip: 192.168.1.44 ip: 192.168.0.100
# [可选] zlm服务器的公网IP, 内网部署置空即可 # [可选] zlm服务器的公网IP, 内网部署置空即可
wanIp: wanIp:
# [可选] zlm服务器的hook所使用的IP, 默认使用sip.ip # [可选] zlm服务器的hook所使用的IP, 默认使用sip.ip
@ -70,12 +70,12 @@ media:
# [可选] zlm服务器的hook.admin_params=secret # [可选] zlm服务器的hook.admin_params=secret
secret: 035c73f7-bb6b-4889-a715-d9eb2d1925cc secret: 035c73f7-bb6b-4889-a715-d9eb2d1925cc
# [可选] zlm服务器的general.streamNoneReaderDelayMS # [可选] zlm服务器的general.streamNoneReaderDelayMS
streamNoneReaderDelayMS: 600000 # 无人观看多久自动关闭流, -1表示永不自动关闭,即 关闭按需拉流 streamNoneReaderDelayMS: 18000 # 无人观看多久自动关闭流, -1表示永不自动关闭,即 关闭按需拉流
# [可选] 自动点播, 使用固定流地址进行播放时,如果未点播则自动进行点播, 需要rtp.enable=true # [可选] 自动点播, 使用固定流地址进行播放时,如果未点播则自动进行点播, 需要rtp.enable=true
autoApplyPlay: true autoApplyPlay: false
# [可选] 部分设备需要扩展SDP,需要打开此设置 # [可选] 部分设备需要扩展SDP,需要打开此设置
seniorSdp: false seniorSdp: false
# 启用udp多端口模式, 详细解释参考: https://github.com/xia-chu/ZLMediaKit/wiki/GB28181%E6%8E%A8%E6%B5%81 下的高阶使用 # 启用udp多端口模式
rtp: rtp:
# [可选] 是否启用udp多端口模式, 开启后会在udpPortRange范围内选择端口用于媒体流传输 # [可选] 是否启用udp多端口模式, 开启后会在udpPortRange范围内选择端口用于媒体流传输
enable: true enable: true

6
web_src/.postcssrc.js

@ -5,6 +5,10 @@ module.exports = {
"postcss-import": {}, "postcss-import": {},
"postcss-url": {}, "postcss-url": {},
// to edit target browsers: use "browserslist" field in package.json // to edit target browsers: use "browserslist" field in package.json
"autoprefixer": {} "autoprefixer": {},
'postcss-pxtorem': {
rootValue: 24,
propList: ['font-size'] // 只转化font-size
}
} }
} }

49
web_src/package-lock.json

@ -99,7 +99,6 @@
"version": "3.2.1", "version": "3.2.1",
"resolved": "https://registry.npm.taobao.org/ansi-styles/download/ansi-styles-3.2.1.tgz", "resolved": "https://registry.npm.taobao.org/ansi-styles/download/ansi-styles-3.2.1.tgz",
"integrity": "sha1-QfuyAkPlCxK+DwS43tvwdSDOhB0=", "integrity": "sha1-QfuyAkPlCxK+DwS43tvwdSDOhB0=",
"dev": true,
"requires": { "requires": {
"color-convert": "^1.9.0" "color-convert": "^1.9.0"
} }
@ -1645,7 +1644,6 @@
"version": "2.4.2", "version": "2.4.2",
"resolved": "https://registry.npm.taobao.org/chalk/download/chalk-2.4.2.tgz", "resolved": "https://registry.npm.taobao.org/chalk/download/chalk-2.4.2.tgz",
"integrity": "sha1-zUJUFnelQzPPVBpJEIwUMrRMlCQ=", "integrity": "sha1-zUJUFnelQzPPVBpJEIwUMrRMlCQ=",
"dev": true,
"requires": { "requires": {
"ansi-styles": "^3.2.1", "ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5", "escape-string-regexp": "^1.0.5",
@ -1847,7 +1845,6 @@
"version": "1.9.3", "version": "1.9.3",
"resolved": "https://registry.npm.taobao.org/color-convert/download/color-convert-1.9.3.tgz", "resolved": "https://registry.npm.taobao.org/color-convert/download/color-convert-1.9.3.tgz",
"integrity": "sha1-u3GFBpDh8TZWfeYp0tVHHe2kweg=", "integrity": "sha1-u3GFBpDh8TZWfeYp0tVHHe2kweg=",
"dev": true,
"requires": { "requires": {
"color-name": "1.1.3" "color-name": "1.1.3"
} }
@ -1855,8 +1852,7 @@
"color-name": { "color-name": {
"version": "1.1.3", "version": "1.1.3",
"resolved": "https://registry.npm.taobao.org/color-name/download/color-name-1.1.3.tgz", "resolved": "https://registry.npm.taobao.org/color-name/download/color-name-1.1.3.tgz",
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU="
"dev": true
}, },
"color-string": { "color-string": {
"version": "0.3.0", "version": "0.3.0",
@ -3713,8 +3709,7 @@
"escape-string-regexp": { "escape-string-regexp": {
"version": "1.0.5", "version": "1.0.5",
"resolved": "https://registry.npm.taobao.org/escape-string-regexp/download/escape-string-regexp-1.0.5.tgz", "resolved": "https://registry.npm.taobao.org/escape-string-regexp/download/escape-string-regexp-1.0.5.tgz",
"integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ="
"dev": true
}, },
"escope": { "escope": {
"version": "3.6.0", "version": "3.6.0",
@ -4150,6 +4145,11 @@
"locate-path": "^2.0.0" "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": { "flatten": {
"version": "1.0.3", "version": "1.0.3",
"resolved": "https://registry.npm.taobao.org/flatten/download/flatten-1.0.3.tgz", "resolved": "https://registry.npm.taobao.org/flatten/download/flatten-1.0.3.tgz",
@ -4403,8 +4403,7 @@
"has-flag": { "has-flag": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npm.taobao.org/has-flag/download/has-flag-3.0.0.tgz", "resolved": "https://registry.npm.taobao.org/has-flag/download/has-flag-3.0.0.tgz",
"integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0="
"dev": true
}, },
"has-symbols": { "has-symbols": {
"version": "1.0.1", "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": { "postcss-reduce-idents": {
"version": "2.4.0", "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", "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": { "source-map": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npm.taobao.org/source-map/download/source-map-0.6.1.tgz", "resolved": "https://registry.npm.taobao.org/source-map/download/source-map-0.6.1.tgz",
"integrity": "sha1-dHIq8y6WFOnCh6jQu95IteLxomM=", "integrity": "sha1-dHIq8y6WFOnCh6jQu95IteLxomM="
"dev": true
}, },
"source-map-resolve": { "source-map-resolve": {
"version": "0.5.3", "version": "0.5.3",
@ -10290,7 +10316,6 @@
"version": "5.5.0", "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", "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=", "integrity": "sha1-4uaaRKyHcveKHsCzW2id9lMO/I8=",
"dev": true,
"requires": { "requires": {
"has-flag": "^3.0.0" "has-flag": "^3.0.0"
} }

2
web_src/package.json

@ -15,7 +15,9 @@
"core-js": "^2.6.5", "core-js": "^2.6.5",
"echarts": "^4.7.0", "echarts": "^4.7.0",
"element-ui": "2.10.1", "element-ui": "2.10.1",
"fingerprintjs2": "^2.1.2",
"moment": "^2.29.1", "moment": "^2.29.1",
"postcss-pxtorem": "^5.1.1",
"vue": "^2.6.11", "vue": "^2.6.11",
"vue-clipboard2": "^0.3.1", "vue-clipboard2": "^0.3.1",
"vue-cookies": "^1.7.4", "vue-cookies": "^1.7.4",

3
web_src/src/components/UiHeader.vue

@ -33,7 +33,8 @@ export default {
sseControl() { sseControl() {
let that = this; let that = this;
if (this.alarmNotify) { 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) { this.sseSource.addEventListener('message', function(evt) {
that.$notify({ that.$notify({
title: '收到报警信息', title: '收到报警信息',

22
web_src/src/main.js

@ -8,10 +8,28 @@ import axios from 'axios';
import VueCookies from 'vue-cookies'; import VueCookies from 'vue-cookies';
import echarts from 'echarts'; import echarts from 'echarts';
import VueClipboard from 'vue-clipboard2' import VueClipboard from 'vue-clipboard2';
import { Notification } from 'element-ui'; 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) { //把微信浏览器里UA的wifi或4G等网络替换成空,不然切换网络会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(ElementUI);
Vue.use(VueCookies); Vue.use(VueCookies);
Vue.prototype.$axios = axios; Vue.prototype.$axios = axios;

Loading…
Cancel
Save