socket.js 2.7 KB

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