App.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <div id="app">
  3. <router-view />
  4. </div>
  5. </template>
  6. <script>
  7. import { mapGetters } from 'vuex'
  8. import messageApi from '@/api/system/message'
  9. export default {
  10. name: 'App',
  11. data() {
  12. return {
  13. url: process.env.VUE_APP_WEBSOCKET_URL + '?uid=',
  14. ws: null,
  15. interValId: '',
  16. timerId: '',
  17. }
  18. },
  19. created() {
  20. this.connectWebsocket()
  21. },
  22. destroyed() {
  23. clearInterval(this.interValId)
  24. },
  25. computed: {
  26. ...mapGetters({
  27. id: 'user/id',
  28. username: 'user/username',
  29. }),
  30. },
  31. methods: {
  32. connectWebsocket() {
  33. if (this.id) {
  34. this.ws = new WebSocket(this.url + this.id)
  35. } else {
  36. this.ws = null
  37. console.info('uid为空')
  38. }
  39. try {
  40. if (this.ws) {
  41. // ws连接成功
  42. this.ws.onopen = function () {
  43. console.info('WebSocket Server [' + this.url + '] 连接成功!')
  44. }
  45. // ws连接关闭
  46. this.ws.onclose = this.onClose
  47. // ws连接错误
  48. this.ws.onerror = this.onError
  49. // ws数据返回处理
  50. this.ws.onmessage = this.onMessage
  51. }
  52. } catch (e) {
  53. console.error(e)
  54. }
  55. this.interValId = setInterval(this.heartAndReconnect, 10000)
  56. },
  57. // 心跳且重新
  58. heartAndReconnect() {
  59. if (this.ws) {
  60. let data = {
  61. type: 'heartbeat',
  62. }
  63. this.ws.send(JSON.stringify(data))
  64. } else {
  65. clearInterval(this.interValId)
  66. this.connectWebsocket()
  67. }
  68. },
  69. // 关闭
  70. onClose() {
  71. if (this.ws) {
  72. this.ws.close()
  73. this.ws = null
  74. }
  75. console.info('WebSocket Server [' + this.url + '] 连接关闭!')
  76. },
  77. // 处理链接错误
  78. onError() {
  79. if (this.ws) {
  80. this.ws.close()
  81. this.ws = null
  82. }
  83. console.info('WebSocket Server [' + this.url + '] 连接错误!')
  84. },
  85. onMessage(result) {
  86. console.info(' > ' + result.data)
  87. this.showMessage(result.data)
  88. },
  89. // 打开消息弹窗
  90. showMessage(item) {
  91. let data = JSON.parse(item)
  92. if (data.type === 'SendMessage' && data.content.body) {
  93. this.$baseEventBus.$emit('receivedMessage')
  94. let msg = JSON.parse(item).content.body
  95. const h = this.$createElement
  96. let tag = {
  97. style: {
  98. cursor: 'pointer',
  99. },
  100. on: {
  101. click: () => {
  102. // this.handleItem(data.content.body)
  103. },
  104. },
  105. }
  106. this.$notify({
  107. title: msg.msgTitle,
  108. position: 'top-right',
  109. type: 'warning',
  110. duration: 5000,
  111. dangerouslyUseHTMLString: true,
  112. message: h('div', {}, [h('div', tag, msg.msgContent)]),
  113. })
  114. }
  115. },
  116. // 执行路由跳转
  117. handleItem(item) {
  118. messageApi.getEntityById({ id: item.id }).then(() => {
  119. this.$baseEventBus.$emit('receivedMessage')
  120. })
  121. },
  122. },
  123. }
  124. </script>