socket.js 2.5 KB

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