websocketMixin.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. export default {
  2. data() {
  3. return {
  4. /* webSocket推送的数据*/
  5. webSocket: null,
  6. webSocketTaskData: {},
  7. showVehivlesTaskId: null,
  8. socketReconnectNum: 0, // 不能超过5次
  9. lockReconnect: false,
  10. connectSocketUrl: '', // socket连接的url
  11. connectSocketName: '', // socket连接的url
  12. // 心跳机制
  13. setHearTime: 10000,
  14. heartTimeOut: 20000,
  15. timeoutObj: null
  16. }
  17. },
  18. methods: {
  19. /* 心跳*/
  20. heartReset() {
  21. this.timeoutObj && clearTimeout(this.timeoutObj)
  22. // this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj)
  23. },
  24. heartStart() {
  25. this.timeoutObj && clearTimeout(this.timeoutObj)
  26. this.timeoutObj = setInterval(() => {
  27. // console.log('发送heartCheck',this.connectSocketName)
  28. if (this.webSocket.readyState !== 1) {
  29. // 重新连接
  30. console.log('重新连接', this.socketReconnectNum)
  31. this.socketReconnect()
  32. } else {
  33. this.webSocket.send('heartCheck' + this.connectSocketName)
  34. }
  35. }, 10000)
  36. },
  37. /* websocket重新连接*/
  38. async socketReconnect() {
  39. // if(this.socketReconnectNum>7||this.lockReconnect) {
  40. // socket断开后会重新连接6次
  41. if (this.socketReconnectNum > 5) {
  42. this.heartReset()
  43. } else {
  44. // console.log('socket重新连接了' + this.connectSocketName+"count"+this.socketReconnectNum)
  45. this.socketReconnectNum++
  46. this.socketConnectMixin()
  47. }
  48. },
  49. resetSocketData() {
  50. this.socketReconnectNum = 0
  51. // this.lockReconnect=false
  52. },
  53. /* 使用方法*/
  54. socketConnectMixin() {
  55. // console.log("socketConnectMixin被执行了",this.socketReconnectNum);
  56. return new Promise((resolve) => {
  57. if (typeof WebSocket === 'undefined') {
  58. console.log('遗憾:您的浏览器不支持WebSocket')
  59. } else {
  60. console.log(' new WebSocket', this.connectSocketUrl)
  61. this.webSocket = new WebSocket(this.connectSocketUrl)
  62. // 连接打开事件
  63. this.webSocket.onopen = () => {
  64. this.heartStart()
  65. resolve(true)
  66. }
  67. // 收到消息事件
  68. this.webSocket.onmessage = (msg) => {
  69. this.heartStart()
  70. this.resetSocketData()
  71. this.webSocketMessage(msg)
  72. }
  73. // 连接关闭事件
  74. this.webSocket.onclose = () => {
  75. // this.heartReset();
  76. // console.log('Socket发生了错误')
  77. // this.lockReconnect = false;
  78. // this.socketReconnect();
  79. console.log('Socket已关闭', this.connectSocketName)
  80. }
  81. // 发生了错误事件
  82. this.webSocket.onerror = () => {
  83. console.log('Socket发生了错误', this.connectSocketName)
  84. // this.lockReconnect = false;
  85. // this.socketReconnect();
  86. }
  87. }
  88. })
  89. },
  90. // webSocket消息监听
  91. webSocketMessage() {},
  92. // 关闭websocket连接
  93. webSocketClose() {
  94. this.webSocket.close()
  95. this.heartReset()
  96. }
  97. }
  98. }