socket.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
  2. const SocketConfig = {
  3. retry: 20,
  4. timeout: 10000,
  5. timeHandler: null,
  6. reset: function() {
  7. clearTimeout(this.timeHandler);
  8. this.timeHandler = null;
  9. return this;
  10. },
  11. start: function(send) {
  12. this.timeHandler = setTimeout(() => {
  13. send && send();
  14. this.reset().start(send);
  15. }, this.timeout);
  16. }
  17. };
  18. class SocketUtil {
  19. constructor({ url, onOpen, onMessage, onClose, onError }) {
  20. this.url = url || `${extConfig.socketApiHost}:8282`;
  21. this.socket = this.connectSocket(this.url);
  22. this.onOpenCall = onOpen;
  23. this.onMessageCall = onMessage;
  24. this.onCloseCall = onClose;
  25. this.onErrorCall = onError;
  26. this.retryTime = 0;
  27. this.reconnectHandler = null;
  28. this.lockReconnect = false;
  29. this.bindHandlerEvent();
  30. // uni.onSocketOpen(function(res) {
  31. // console.log("WebSocket连接已打开!");
  32. // });
  33. // uni.onSocketMessage(function(res) {
  34. // console.log("收到服务器内容:" + res.data);
  35. // });
  36. // uni.onSocketOpen(function() {
  37. // uni.closeSocket();
  38. // });
  39. }
  40. bindHandlerEvent() {
  41. this.socket.onOpen(this.openSocket);
  42. // 监听消息
  43. this.socket.onMessage(this.onMessageSocket);
  44. // 监听Socket的关闭
  45. this.socket.onClose(this.closeSocket);
  46. this.socket.onError(this.errorSocket);
  47. }
  48. connectSocket(url) {
  49. let path = `ws://${url}`;
  50. // #ifdef MP-WEIXIN
  51. path = `wss://${url}`;
  52. // #endif
  53. const task = uni.connectSocket({
  54. url: path,
  55. complete: () => {}
  56. });
  57. return task;
  58. }
  59. openSocket = event => {
  60. console.log("contact ok", event);
  61. SocketConfig.reset().start(() => {
  62. this.send("ping");
  63. });
  64. this.onOpenCall && this.onOpenCall(event);
  65. };
  66. onMessageSocket = data => {
  67. console.log("Client received a message", data);
  68. SocketConfig.reset().start(() => {
  69. this.send("ping");
  70. });
  71. try {
  72. let info = JSON.parse(data.data);
  73. this.onMessageCall && this.onMessageCall(info);
  74. } catch (error) {}
  75. };
  76. errorSocket(err) {
  77. console.log("contact error", err);
  78. this.onErrorCall && this.onErrorCall(err);
  79. }
  80. closeSocket() {
  81. console.log("Client notified socket has closed");
  82. this.onCloseCall && this.onCloseCall();
  83. }
  84. send(data) {
  85. this.socket.send({
  86. data: JSON.stringify(data)
  87. });
  88. }
  89. colse() {
  90. this.socket.close();
  91. }
  92. reconnect() {
  93. if (this.lockReconnect) return;
  94. this.lockReconnect = true;
  95. clearTimeout(this.reconnectHandler);
  96. if (this.retryTime < SocketConfig.retry) {
  97. this.reconnectHandler = setTimeout(() => {
  98. this.socket = this.connectSocket(this.url);
  99. this.bindHandlerEvent();
  100. this.lockReconnect = false;
  101. }, 5000);
  102. this.retryTime++;
  103. }
  104. }
  105. }
  106. export default SocketUtil;