socket.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import store from '@/store/index.js'
  2. import method from '@/common/methodTool'
  3. let isSocketClose = false; // 是否关闭socket
  4. let heartbeatInterval = null; // 心跳定时器
  5. let socketTask = null; // websocket对象
  6. let againTimer = null; //断线重连定时器
  7. let url = null;
  8. /**
  9. * sockeUrl:websocet的地址
  10. * */
  11. const sokcet = (sockeUrl) => {
  12. url = sockeUrl;
  13. isSocketClose = false;
  14. //判断是否有websocet对象,有的话清空
  15. if (socketTask) {
  16. socketTask.close();
  17. socketTask = null;
  18. clearInterval(heartbeatInterval);
  19. }
  20. // 连接
  21. console.log(url)
  22. socketTask = uni.connectSocket({
  23. url: url,
  24. success(data) {
  25. console.log("websocket连接成功");
  26. clearInterval(againTimer) //断线重连定时器
  27. },
  28. fail: (err) => {
  29. console.log("报错", err);
  30. }
  31. });
  32. // 连接打开
  33. socketTask.onOpen((res) => {
  34. console.log('WebSocket打开');
  35. clearInterval(againTimer) //断线重连定时器
  36. clearInterval(heartbeatInterval);
  37. // // 30秒发送一次心跳
  38. // heartbeatInterval = setInterval(() => {
  39. // sendMsg('心跳ing')
  40. // }, 30000)
  41. })
  42. // 监听连接失败
  43. socketTask.onError((err) => {
  44. console.log('WebSocket连接打开失败,请检查', err);
  45. //停止发送心跳
  46. clearInterval(heartbeatInterval)
  47. //如果不是人为关闭的话,进行重连
  48. if (!isSocketClose) {
  49. reconnect(url)
  50. }
  51. })
  52. // // 监听连接关闭 -
  53. socketTask.onClose((e) => {
  54. console.log('WebSocket连接关闭!',e);
  55. clearInterval(heartbeatInterval)
  56. if (!isSocketClose) {
  57. reconnect(url)
  58. }
  59. })
  60. // 监听收到信息
  61. socketTask.onMessage((res) => {
  62. console.log(res, 'res监听收到信息')
  63. if(res.data == 'offLine') {
  64. stop();
  65. uni.setStorageSync('needToLogin','1')
  66. uni.showToast({
  67. icon:'none',
  68. title:'用户在其他终端登录,强制下线',
  69. duration:3000,
  70. })
  71. setTimeout(() => {
  72. method.exit();
  73. },3000)
  74. }
  75. });
  76. }
  77. const reconnect = (url) => {
  78. console.log('进入断线重连', isSocketClose);
  79. clearInterval(againTimer) //断线重连定时器
  80. clearInterval(heartbeatInterval);
  81. console.log(socketTask)
  82. socketTask && socketTask.close(); // 确保已经关闭后再重新打开
  83. socketTask = null;
  84. // 连接 重新调用创建websocet方法
  85. againTimer = setInterval(() => {
  86. sokcet(url)
  87. console.log('在重新连接中...');
  88. }, 20000)
  89. }
  90. const sendMsg = (msg) => { //向后端发送心跳包
  91. console.log(msg)
  92. try {
  93. //通过 WebSocket 连接发送数据
  94. socketTask.send({
  95. data:msg
  96. });
  97. } catch (e) {
  98. console.log(e,'msg')
  99. if (isSocketClose) {
  100. return
  101. } else {
  102. reconnect(url)
  103. }
  104. }
  105. }
  106. const stop = () => {
  107. isSocketClose = true
  108. clearInterval(heartbeatInterval);
  109. clearInterval(againTimer) //断线重连定时器
  110. socketTask && socketTask.close(); // 确保已经关闭后再重新打开
  111. socketTask = null;
  112. }
  113. export const websocket = {
  114. sokcet,
  115. stop,
  116. sendMsg
  117. };