utils-uniapp 1.2.14 → 1.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const c=require("../_rollupPluginBabelHelpers-3cc6d021.js");var
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const c=require("../_rollupPluginBabelHelpers-3cc6d021.js");var m={create:function(t){return new WebSocket(t)},bind:function(t,e){t.onopen=e.open,t.onmessage=e.message,t.onclose=e.close,t.onerror=e.error},unbind:function(t){t.onopen=t.onmessage=t.onclose=t.onerror=null},send:function(t,e){t.send(e)},close:function(t,e,n){t.close(e,n)}},u={create:function(t){var e=uni.connectSocket({url:t,complete:function(){}});if(!e||typeof e.onOpen!="function")throw new Error("uni.connectSocket did not return a valid SocketTask");return e},bind:function(t,e){t.onOpen(e.open),t.onMessage(e.message),t.onClose(e.close),t.onError(e.error)},unbind:function(){},send:function(t,e){t.send({data:e})},close:function(t,e,n){e!=null?t.close({code:e,reason:n||""}):t.close({})}},l=function(){function s(t){var e,n,r=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},o=arguments.length>2?arguments[2]:void 0;c._classCallCheck(this,s),this.url=t,this.adapter=r.adapter||o||(typeof uni!="undefined"?u:m),this.reconnectDelay=r.reconnectDelay||1e3,this.maxReconnectDelay=r.maxReconnectDelay||3e4,this.backoffFactor=r.backoffFactor||2,this.jitter=(e=r.jitter)!==null&&e!==void 0?e:.3,this.maxReconnect=(n=r.maxReconnect)!==null&&n!==void 0?n:-1,this.stablePeriod=r.stablePeriod||60*1e3,this.maxDrops=r.maxDrops||5,this.heartbeatInterval=r.heartbeatInterval||3e4,this.heartbeatTimeout=r.heartbeatTimeout||1e4,this.heartbeatMsg=r.heartbeatMsg||"ping",this.connectTimeout=r.connectTimeout||15e3,this.transport=null,this.isConnected=!1,this.manualClose=!1,this.heartbeatTimer=null,this.heartbeatTimeoutTimer=null,this.reconnectTimer=null,this.connectTimeoutTimer=null,this.reconnectCount=0,this.dropCount=0,this.lastDropTime=0,this.connectedAt=0,this.dropCycle=0,this.handledCycle=0,this.events={open:[],message:[],close:[],error:[],reconnect:[],reconnecting:[],fail:[]}}return c._createClass(s,[{key:"on",value:function(e,n){return this.events[e]&&this.events[e].push(n),this}},{key:"off",value:function(e,n){if(!this.events[e])return this;var r=this.events[e],o=r.findIndex(function(i){return i===n||i._origin===n});return o>-1&&r.splice(o,1),this}},{key:"once",value:function(e,n){var r,o=this;if(!((r=this.events[e])!==null&&r!==void 0&&r.some(function(a){return a===n||a._origin===n}))){var i=function(){o.off(e,i),n.apply(void 0,arguments)};i._origin=n,this.on(e,i)}return this}},{key:"emit",value:function(e){for(var n=arguments.length,r=new Array(n>1?n-1:0),o=1;o<n;o++)r[o-1]=arguments[o];this.events[e]&&this.events[e].forEach(function(i){try{i.apply(void 0,r)}catch(a){console.error("[".concat(e,"] callback error:"),a)}})}},{key:"addEventListener",value:function(e,n){return this.on(e,n)}},{key:"removeEventListener",value:function(e,n){return this.off(e,n)}},{key:"connect",value:function(){var e=this;if(this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.clearConnectTimeout(),this.transport){var n=this.transport;this.transport=null,this.isConnected=!1,this.stopHeartbeat(),this.adapter.unbind(n);try{this.adapter.close(n,1e3,"reconnect")}catch(i){}}this.manualClose=!1;var r=this.dropCycle=this.dropCycle+1,o;try{o=this.adapter.create(this.url)}catch(i){return this.handleDrop(r,{type:"connectError",url:this.url,event:i}),this}if(o==null)return this.handleDrop(r,{type:"connectError",url:this.url,event:new Error("adapter.create returned no transport")}),this;this.transport=o,this.connectTimeoutTimer=setTimeout(function(){if(!(o!==e.transport||e.isConnected||e.manualClose)){console.warn("Connect timeout"),e.handleDrop(r,{type:"connectTimeout",url:e.url});try{e.adapter.close(o)}catch(i){}}},this.connectTimeout);try{this.adapter.bind(o,{open:function(a){if(o===e.transport&&!e.manualClose){e.clearConnectTimeout(),e.reconnectTimer&&(clearTimeout(e.reconnectTimer),e.reconnectTimer=null),e.handledCycle===r&&(e.handledCycle=0);var h=e.reconnectCount>0;e.isConnected=!0,e.connectedAt=Date.now(),e.reconnectCount=0,console.log("Connection established"),e.emit("open",a),h&&(console.log("Reconnected successfully"),e.emit("reconnect",a)),e.startHeartbeat()}},message:function(a){o===e.transport&&(e.clearHeartbeatTimeout(),e.emit("message",a))},close:function(a){o===e.transport&&(e.clearConnectTimeout(),e.isConnected=!1,e.stopHeartbeat(),e.transport=null,console.log("Connection closed"),e.emit("close",a),!e.manualClose&&e.handleDrop(r))},error:function(a){if(o===e.transport){e.clearConnectTimeout(),console.error("WebSocket error:",a),e.emit("error",a),e.isConnected=!1,e.stopHeartbeat(),e.manualClose||e.handleDrop(r,{type:"connectError",url:e.url,event:a});try{e.adapter.close(o,1e3,"error")}catch(h){}}}})}catch(i){this.clearConnectTimeout(),this.transport=null;try{this.adapter.close(o)}catch(a){}return this.handleDrop(r,{type:"connectError",url:this.url,event:i}),this}return this}},{key:"handleDrop",value:function(e,n){if(!this.manualClose&&e===this.dropCycle&&this.handledCycle!==e){this.handledCycle=e;var r=this.connectedAt>0,o=r?Date.now()-this.connectedAt:0;if(this.connectedAt=0,r&&(o>=this.stablePeriod&&(this.dropCount=0),this.dropCount++,this.lastDropTime=Date.now(),console.warn("Drop #".concat(this.dropCount," (uptime ").concat((o/1e3).toFixed(1),"s)")),this.dropCount>this.maxDrops)){this.emit("fail",{type:"tooManyDrops",dropCount:this.dropCount,stablePeriod:this.stablePeriod,willRetry:!1}),this.dropCount=0;return}var i=this.reconnect();n&&this.emit("fail",c._objectSpread2(c._objectSpread2({},n),{},{willRetry:i}))}}},{key:"reconnect",value:function(){var e=this;if(this.isConnected||this.reconnectTimer)return!1;if(this.maxReconnect!==-1&&this.reconnectCount>=this.maxReconnect)return console.log("已到达最大重连次数(".concat(this.maxReconnect,"),停止重连")),this.emit("fail",{type:"maxReconnect",maxReconnect:this.maxReconnect,url:this.url,willRetry:!1}),!1;var n=this.getBackoffDelay();return this.reconnectCount+=1,console.log("Reconnecting in ".concat((n/1e3).toFixed(1),"s... count:").concat(this.reconnectCount)),this.emit("reconnecting",{delay:n,count:this.reconnectCount}),this.reconnectTimer=setTimeout(function(){e.reconnectTimer=null,e.connect()},n),!0}},{key:"getBackoffDelay",value:function(){var e=Math.max(this.reconnectCount,0),n=this.reconnectDelay*Math.pow(this.backoffFactor,e);return n=Math.min(n,this.maxReconnectDelay),this.jitter>0&&(n+=(Math.random()*2-1)*n*this.jitter),Math.max(Math.round(n),100)}},{key:"send",value:function(e){return this.isConnected&&this.transport?this.adapter.send(this.transport,e):console.error("WebSocket is not connected"),this}},{key:"startHeartbeat",value:function(){var e=this;this.stopHeartbeat(),this.heartbeatTimer=setInterval(function(){e.isConnected&&(e.send(e.heartbeatMsg),e.startHeartbeatTimeout())},this.heartbeatInterval)}},{key:"stopHeartbeat",value:function(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null),this.clearHeartbeatTimeout()}},{key:"startHeartbeatTimeout",value:function(){var e=this;this.clearHeartbeatTimeout();var n=this.transport;this.heartbeatTimeoutTimer=setTimeout(function(){if(!(n!==e.transport||!e.isConnected)){console.warn("Heartbeat timeout, connection dead"),e.isConnected=!1,e.stopHeartbeat(),e.handleDrop(e.dropCycle,{type:"heartbeatTimeout"});try{e.adapter.close(n,1e3,"heartbeat")}catch(r){}}},this.heartbeatTimeout)}},{key:"clearHeartbeatTimeout",value:function(){this.heartbeatTimeoutTimer&&(clearTimeout(this.heartbeatTimeoutTimer),this.heartbeatTimeoutTimer=null)}},{key:"clearConnectTimeout",value:function(){this.connectTimeoutTimer&&(clearTimeout(this.connectTimeoutTimer),this.connectTimeoutTimer=null)}},{key:"close",value:function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:1e3,n=arguments.length>1&&arguments[1]!==void 0?arguments[1]:"";if(this.manualClose=!0,this.isConnected=!1,this.stopHeartbeat(),this.clearConnectTimeout(),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.reconnectCount=0,this.dropCount=0,this.connectedAt=0,this.dropCycle=this.dropCycle+1,this.transport)try{this.adapter.close(this.transport,e,n)}catch(r){try{this.adapter.close(this.transport)}catch(o){}}}},{key:"getStats",value:function(){return{connected:this.isConnected,reconnectCount:this.reconnectCount,dropCount:this.dropCount,lastDropTime:this.lastDropTime,uptime:this.isConnected?Date.now()-this.connectedAt:0}}}])}(),d=function(s){function t(e){var n=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};return c._classCallCheck(this,t),c._callSuper(this,t,[e,n,u])}return c._inherits(t,s),c._createClass(t)}(l);exports.BrowserWebSocketAdapter=m;exports.UniWebSocketAdapter=u;exports.UniWebSocketClient=d;exports.WebSocketClient=l;exports.default=l;
|