message.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { patternNginxWsUrl, getWsUrl } from "../../utils/ws"
  2. // const webSocket = require('ws')
  3. // const wsUrl = patternNginxWsUrl()
  4. const wsUrl = getWsUrl()
  5. const useMessageStore = defineStore('message', {
  6. status: () => ({
  7. // 未读消息数量
  8. messageNum: 0,
  9. ws: null,
  10. latestMessage: null,
  11. heartTimeOut: 40000,
  12. lockReconnect: false,
  13. timerReconnect: undefined,
  14. timerHeart: undefined,
  15. timerServerHeart: undefined,
  16. handClose: false,
  17. messageCount: 0,
  18. id: undefined,
  19. token: token
  20. }),
  21. actions: {
  22. getMessageNum(message) {
  23. this.messageNum++
  24. this.latestMessage = message
  25. // console.log(this.messageNum)
  26. // console.log(this.latestMessage)
  27. this.messageCount = this.latestMessage.data
  28. const value = this.messageCount != null ? Number(this.messageCount) : 0
  29. this.broadcastMessage(value)
  30. },
  31. broadcastMessage(count) {
  32. const event = new CustomEvent('messageChange', { detail: count });
  33. window.dispatchEvent(event);
  34. },
  35. acceptWs: () => {
  36. },
  37. async createWebSocket(id, token) {
  38. // console.log(id, wsUrl, token)
  39. try {
  40. this.ws = new WebSocket(wsUrl + id, [token])
  41. // this.initWebsocket()
  42. } catch (e) {
  43. // console.log(e)
  44. this.reconnection(id, token)
  45. }
  46. },
  47. async connection(id, token) {
  48. this.id = id
  49. this.token = token
  50. // this.getMessageNum('1')
  51. // console.log(id, wsUrl, token)
  52. // console.log(window)
  53. // if("webSocket" in window) {
  54. await this.createWebSocket(id, token).then(_ => {
  55. this.initWebsocket()
  56. })
  57. // } else {
  58. // console.log('浏览器不支持websocket')
  59. // }
  60. },
  61. initWebsocket() {
  62. this.ws.onopen = (e) => {
  63. this.ws.send("open server")
  64. console.log("链接成功")
  65. this.heartCheck()
  66. }
  67. this.ws.onmessage = (message) => {
  68. this.getMessageNum(message)
  69. this.heartCheck()
  70. }
  71. this.ws.onerror = (e) => {
  72. console.log('链接失败')
  73. this.reconnection(this.id, this.token)
  74. }
  75. this.ws.onclose = (e) => {
  76. console.log("关闭连接")
  77. if (!this.handClose) {
  78. this.reconnection(this.id, this.token)
  79. }
  80. }
  81. },
  82. clearTimer() {
  83. this.timerReconnect && clearTimeout(this.timerReconnect)
  84. this.timerHeart && clearTimeout(this.timerHeart)
  85. this.timerServerHeart && clearTimeout(this.timerServerHeart)
  86. },
  87. reconnection(id, token) {
  88. console.log("重新连接")
  89. if (this.lockReconnect) {
  90. return
  91. }
  92. this.lockReconnect = true
  93. if (this.timerReconnect) {
  94. this.clearTimer(this.timerReconnect)
  95. }
  96. //没连上会一直重连, 设置迟延,避免请求过多
  97. this.timerReconnect = setTimeout(() => {
  98. //setTimeout 到点了执行
  99. this.connection(id, token)
  100. this.lockReconnect = false
  101. }, 5000);
  102. },
  103. heartCheck() {
  104. console.log('心跳检测')
  105. if (this.timerHeart) {
  106. clearTimeout(this.timerHeart)
  107. }
  108. this.timerHeart = setTimeout(() => {
  109. // console.log("ping")
  110. this.ws.send("ping")
  111. this.lockReconnect = false
  112. }, this.heartTimeOut)
  113. },
  114. sendMsg(data) {
  115. console.log("发送信息")
  116. if (this.ws.readyState === WebSocket.OPEN) {
  117. this.ws.send(JSON.stringify(data))
  118. }
  119. },
  120. closeWs() {
  121. console.log("关闭ws")
  122. this.handClose = true
  123. this.clearTimer()
  124. this.ws.close()
  125. },
  126. initMessageCount() {
  127. }
  128. }
  129. })
  130. export default useMessageStore