浏览代码

镜屏多订阅问题

wangkang3 4 年之前
父节点
当前提交
267a642624
共有 2 个文件被更改,包括 48 次插入29 次删除
  1. 47 15
      ebc-middle-platform/src/ipu-stomp.js
  2. 1 14
      ebc-middle-platform/src/modules/layouts/BasicLayout.vue

+ 47 - 15
ebc-middle-platform/src/ipu-stomp.js

@ -4,32 +4,64 @@ import {socketUrl} from '@/constants'
4 4
import EventBus from './bus'
5 5
6 6
var socket = null
7
var stompClient = null
7
var peronStompClient = null
8
var shipStompClient = null
8 9
9 10
export default function initWebSocket(params) {
11
  personStomp(params)
12
  shipStomp(params)
13
}
14
15
function personStomp(params) {
10 16
  if (socketUrl.startsWith('http') || socketUrl.startsWith('https')) {
11 17
    socket = new SockJS(socketUrl)// 连接服务端
12
    stompClient = Stomp.over(socket)
18
    peronStompClient = Stomp.over(socket)
13 19
  } else { // SockJS支持ws前缀
14
    stompClient = Stomp.client(socketUrl)
20
    peronStompClient = Stomp.client(socketUrl)
15 21
  }
16
  stompClient.connect(params, (frame) => {
22
  params.uuid = uuid()
23
  peronStompClient.connect(params, (frame) => {
17 24
    successCallback()
18 25
  }, () => {
19
    initWebSocket(params)
26
    personStomp(params)
20 27
  })
21
  return stompClient
22 28
}
23 29
24
function successCallback() {
25
  stompClient.subscribe('ship', msg => {
26
    // EventBus.$emit('ship', msg)
30
function shipStomp(params) {
31
  if (socketUrl.startsWith('http') || socketUrl.startsWith('https')) {
32
    socket = new SockJS(socketUrl)// 连接服务端
33
    shipStompClient = Stomp.over(socket)
34
  } else { // SockJS支持ws前缀
35
    shipStompClient = Stomp.client(socketUrl)
36
  }
37
  params.uuid = uuid()
38
  shipStompClient.connect(params, (frame) => {
39
    successShipCallback()
40
  }, () => {
41
    shipStomp(params)
27 42
  })
28
  stompClient.subscribe('personnel', msg => { // 订阅多个topic暂时实现
29
    if (msg.headers.destination === 'personnel') {
30
      EventBus.$emit('person', msg.body)
31
    } else if (msg.headers.destination === 'ship') {
32
      EventBus.$emit('ship', msg.body)
33
    }
43
}
44
function successShipCallback() {
45
  shipStompClient.subscribe('ship', msg => {
46
    EventBus.$emit('ship', msg.body)
47
  })
48
}
49
function successCallback() {
50
  peronStompClient.subscribe('personnel', msg => {
51
    EventBus.$emit('person', msg.body)
34 52
  })
35 53
}
54
55
function uuid() {
56
  var s = []
57
  var hexDigits = '0123456789abcdef'
58
  for (var i = 0; i < 36; i++) {
59
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
60
  }
61
  s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
62
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
63
  s[8] = s[13] = s[18] = s[23] = '-'
64
65
  var uuid = s.join('')
66
  return uuid
67
}

+ 1 - 14
ebc-middle-platform/src/modules/layouts/BasicLayout.vue

@ -67,7 +67,7 @@ export default {
67 67
  },
68 68
  mounted() {
69 69
    // 镜屏调用
70
    initWebSocket({name: 'zhangsan', passcode: '123456', uuid: this.uuid()})
70
    initWebSocket({name: 'zhangsan', passcode: '123456'})
71 71
    EventBus.$on('person', (msg) => { // 获取镜屏推送消息
72 72
      console.log(msg)
73 73
      var obj = JSON.parse(msg)
@ -82,19 +82,6 @@ export default {
82 82
  methods: {
83 83
    closeAlarmModal() {
84 84
      this.modal = false
85
    },
86
    uuid() {
87
      var s = []
88
      var hexDigits = '0123456789abcdef'
89
      for (var i = 0; i < 36; i++) {
90
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
91
      }
92
      s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
93
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
94
      s[8] = s[13] = s[18] = s[23] = '-'
95
96
      var uuid = s.join('')
97
      return uuid
98 85
    }
99 86
  }
100 87
}