websockUtil.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. export default {
  2. data() {
  3. return {
  4. userInfo: {
  5. alias: '',
  6. avatar: '',
  7. city: '',
  8. uid: '',
  9. count: 0,
  10. isAdmin: ''
  11. },
  12. unreadMessage: 0, // 心跳检测接受消息失败次数
  13. sendFailCount: 0, // 心跳检测发送消息失败次数
  14. sendData: {
  15. fromUid: '',
  16. context: '测试心跳'
  17. },
  18. uid: '',
  19. SocketTask: {},
  20. lockReconnect: false, // 避免重复连接
  21. }
  22. },
  23. methods: {
  24. // 创建连接
  25. createWebSocket(uid) {
  26. try {
  27. this.uid = uid
  28. this.sendData.fromUid = uid
  29. this.SocketTask = wx.connectSocket({
  30. url: `${uni.$wsLocation}/education/chat?uid=`+uid,
  31. success: res => {
  32. // console.log('websocket连接成功')
  33. if (this.userInfo.count == 0) {
  34. uni.removeTabBarBadge({
  35. index: 2
  36. })
  37. } else {
  38. // 调用 uni.setTabBarBadge() 方法,为购物车设置右上角的徽标
  39. uni.setTabBarBadge({
  40. index: 2,
  41. text: this.userInfo.count + '', // 注意:text 的值必须是字符串,不能是数字
  42. })
  43. }
  44. },
  45. fail: e => {
  46. this.reconnect()
  47. }
  48. })
  49. uni.$SocketTask = this.SocketTask
  50. // 监听打开事件
  51. this.startWebSocket()
  52. } catch(e) {
  53. this.reconnect()
  54. }
  55. },
  56. // 监听开始事件
  57. startWebSocket() {
  58. // console.log('websock监听开启')
  59. this.SocketTask.onOpen(() => {
  60. this.heartCheck()
  61. this.receiveMessage()
  62. })
  63. },
  64. // 重连操作
  65. reconnect() {
  66. // console.log('重连')
  67. if (this.lockReconnect) {
  68. return;
  69. }
  70. this.lockReconnect = true
  71. //没连接上会一直重连,设置延迟避免请求过多
  72. tt && clearTimeout(tt)
  73. let tt = setTimeout(() => {
  74. this.createWebSocket(this.uid)
  75. this.lockReconnect = false
  76. }, 4000)
  77. },
  78. // 心跳检测
  79. heartCheck() {
  80. send && clearInterval(send)
  81. let send = setInterval(() => {
  82. this.SocketTask.send({
  83. data: JSON.stringify(this.sendData),
  84. success: () => {
  85. // console.log('发送成功')
  86. if (this.unreadMessage < 2) {
  87. this.unreadMessage++
  88. } else {
  89. clearInterval(send)
  90. this.SocketTask.close()
  91. this.reconnect()
  92. }
  93. },
  94. fail: () => {
  95. // console.log('发送失败')
  96. if (this.sendFailCount < 2) {
  97. // 发送数据失败,失败次数+1
  98. this.sendFailCount++
  99. } else {
  100. // 发送数据失败次数大于等于2,销毁重连
  101. clearInterval(send)
  102. this.SocketTask.close()
  103. this.reconnect()
  104. }
  105. }
  106. })
  107. }, 60*1000);
  108. },
  109. // 接受服务端消息
  110. receiveMessage() {
  111. this.SocketTask.onMessage( res => {
  112. if (res.data !== '测试心跳') {
  113. // console.log('监听',res.data)
  114. this.userInfo.count++;
  115. this.updateUserInfo(this.userInfo)
  116. } else {
  117. // 测试心跳
  118. this.unreadMessage--
  119. // console.log('收到消息',res.data)
  120. }
  121. })
  122. }
  123. }
  124. }