websocket-front
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,1 +0,1 @@ | ||
define((function(){"use strict";class e{url;opts;ws=null;_reconnectTimer=null;_manualClose=!1;_repeat=1/0;_lockReconnect=!1;_heartbeatTimer=null;_pongTimer=null;constructor(e,t){this.url=e,this.opts={isReconnect:t?.isReconnect||!0,onMessage:t?.onMessage,onError:t?.onError,close:t?.close,reconnectTimeout:t?.reconnectTimeout||300,reconnectRepeat:t?.reconnectRepeat||1/0,isHeartbeat:t?.isHeartbeat||!0,pingMsg:t?.pingMsg||"ping",pingTimeout:t?.pingTimeout||3e4,pongTimeout:t?.pongTimeout||300},this._init()}_init=()=>{this._reconnectTimer=null,this._manualClose=!1,this._repeat=0,this._lockReconnect=!1,this._heartbeatTimer=null,this._pongTimer=null,this._resetReconnect(),this._resetHeartbeat(),this._connect(),this._onopen(),this._onmessage(),this._onerror(),this._onclose()};_connect=()=>{this.ws=new WebSocket(this.url)};_onopen=()=>{this.ws&&(this.ws.onopen=()=>{this._checkHeartbeat()})};_onmessage=()=>{this.ws&&(this.ws.onmessage=e=>{this._checkHeartbeat(),"function"==typeof this.opts.onMessage&&this.opts.onMessage(e)})};_onerror=()=>{this.ws&&(this.ws.onerror=e=>{this._reconnect(),"function"==typeof this.opts.onError&&this.opts.onError(e)})};_onclose=()=>{this.ws&&(this.ws.onclose=e=>{this.opts.isReconnect&&!this._manualClose?this._reconnect():this.ws?.close(),"function"==typeof this.opts.close&&this.opts.close(e)})};_reconnect=()=>{this._resetHeartbeat(),!this.ws||!this.opts.isReconnect||this._lockReconnect||Number(this.opts?.reconnectRepeat)<=this._repeat||this._manualClose||(this._lockReconnect=!0,this._repeat++,this._reconnectTimer=setTimeout((()=>{this._init(),this._lockReconnect=!1}),this.opts?.reconnectTimeout))};_resetReconnect=()=>{this._reconnectTimer&&clearTimeout(this._reconnectTimer)};_checkHeartbeat=()=>{this._resetHeartbeat(),this._startHeartbeat()};_resetHeartbeat=()=>{this._heartbeatTimer&&clearTimeout(this._heartbeatTimer),this._pongTimer&&clearTimeout(this._pongTimer)};_startHeartbeat=()=>{this.opts.isHeartbeat&&this.opts.isReconnect&&(this._heartbeatTimer=setTimeout((()=>{this.ws&&this.ws.readyState===WebSocket.OPEN?this.ws.send(this.opts.pingMsg):this.ws?.close(),this._pongTimer=setTimeout((()=>{this.ws?.close()}),this.opts.pongTimeout)}),this.opts.pingTimeout))};sendMessage=e=>{if(this.ws){if(this.ws.readyState===WebSocket.CONNECTING){const t=setInterval((()=>{this.ws&&(this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e),this.ws.readyState!==WebSocket.CONNECTING&&clearInterval(t))}),100)}this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e)}};close=()=>{this._manualClose=!0,this.ws?.close()}}return"undefined"!=typeof window&&(window.Socket=e),e})); | ||
define((function(){"use strict";class e{url;opts;ws=null;_reconnectTimer=null;_manualClose=!1;_repeat=1/0;_lockReconnect=!1;_heartbeatTimer=null;_pongTimer=null;constructor(e,t){this.url=e,this.opts={isReconnect:t?.isReconnect??!0,onMessage:t?.onMessage,onError:t?.onError,close:t?.close,reconnectTimeout:t?.reconnectTimeout??300,reconnectRepeat:t?.reconnectRepeat??1/0,isHeartbeat:t?.isHeartbeat??!0,pingMsg:t?.pingMsg??"ping",pingTimeout:t?.pingTimeout??3e4,pongTimeout:t?.pongTimeout??300},this._init()}_init=()=>{this._reconnectTimer=null,this._manualClose=!1,this._repeat=0,this._lockReconnect=!1,this._heartbeatTimer=null,this._pongTimer=null,this._resetReconnect(),this._resetHeartbeat(),this._connect(),this._onopen(),this._onmessage(),this._onerror(),this._onclose()};_connect=()=>{this.ws=new WebSocket(this.url)};_onopen=()=>{this.ws&&(this.ws.onopen=()=>{this._checkHeartbeat()})};_onmessage=()=>{this.ws&&(this.ws.onmessage=e=>{this._checkHeartbeat(),"function"==typeof this.opts?.onMessage&&this.opts.onMessage(e)})};_onerror=()=>{this.ws&&(this.ws.onerror=e=>{this._reconnect(),"function"==typeof this.opts?.onError&&this.opts.onError(e)})};_onclose=()=>{this.ws&&(this.ws.onclose=e=>{this.opts.isReconnect&&!this._manualClose?this._reconnect():this.ws?.close(),"function"==typeof this.opts?.close&&this.opts.close(e)})};_reconnect=()=>{this._resetHeartbeat(),!this.ws||!this.opts.isReconnect||this._lockReconnect||Number(this.opts?.reconnectRepeat)<=this._repeat||this._manualClose||(this._lockReconnect=!0,this._repeat++,this._reconnectTimer=setTimeout((()=>{this._init(),this._lockReconnect=!1}),this.opts?.reconnectTimeout))};_resetReconnect=()=>{this._reconnectTimer&&clearTimeout(this._reconnectTimer)};_checkHeartbeat=()=>{this._resetHeartbeat(),this._startHeartbeat()};_resetHeartbeat=()=>{this._heartbeatTimer&&clearTimeout(this._heartbeatTimer),this._pongTimer&&clearTimeout(this._pongTimer)};_startHeartbeat=()=>{this.opts.isHeartbeat&&this.opts.isReconnect&&(this._heartbeatTimer=setTimeout((()=>{this.ws&&this.ws.readyState===WebSocket.OPEN?this.ws.send(this.opts.pingMsg):this.ws?.close(),this._pongTimer=setTimeout((()=>{this.ws?.close()}),this.opts.pongTimeout)}),this.opts.pingTimeout))};sendMessage=e=>{if(this.ws){if(this.ws.readyState===WebSocket.CONNECTING){const t=setInterval((()=>{this.ws&&(this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e),this.ws.readyState!==WebSocket.CONNECTING&&clearInterval(t))}),100)}this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e)}};close=()=>{this._manualClose=!0,this.ws?.close()}}return"undefined"!=typeof window&&(window.Socket=e),e})); |
@@ -1,1 +0,1 @@ | ||
"use strict";class e{url;opts;ws=null;_reconnectTimer=null;_manualClose=!1;_repeat=1/0;_lockReconnect=!1;_heartbeatTimer=null;_pongTimer=null;constructor(e,t){this.url=e,this.opts={isReconnect:t?.isReconnect||!0,onMessage:t?.onMessage,onError:t?.onError,close:t?.close,reconnectTimeout:t?.reconnectTimeout||300,reconnectRepeat:t?.reconnectRepeat||1/0,isHeartbeat:t?.isHeartbeat||!0,pingMsg:t?.pingMsg||"ping",pingTimeout:t?.pingTimeout||3e4,pongTimeout:t?.pongTimeout||300},this._init()}_init=()=>{this._reconnectTimer=null,this._manualClose=!1,this._repeat=0,this._lockReconnect=!1,this._heartbeatTimer=null,this._pongTimer=null,this._resetReconnect(),this._resetHeartbeat(),this._connect(),this._onopen(),this._onmessage(),this._onerror(),this._onclose()};_connect=()=>{this.ws=new WebSocket(this.url)};_onopen=()=>{this.ws&&(this.ws.onopen=()=>{this._checkHeartbeat()})};_onmessage=()=>{this.ws&&(this.ws.onmessage=e=>{this._checkHeartbeat(),"function"==typeof this.opts.onMessage&&this.opts.onMessage(e)})};_onerror=()=>{this.ws&&(this.ws.onerror=e=>{this._reconnect(),"function"==typeof this.opts.onError&&this.opts.onError(e)})};_onclose=()=>{this.ws&&(this.ws.onclose=e=>{this.opts.isReconnect&&!this._manualClose?this._reconnect():this.ws?.close(),"function"==typeof this.opts.close&&this.opts.close(e)})};_reconnect=()=>{this._resetHeartbeat(),!this.ws||!this.opts.isReconnect||this._lockReconnect||Number(this.opts?.reconnectRepeat)<=this._repeat||this._manualClose||(this._lockReconnect=!0,this._repeat++,this._reconnectTimer=setTimeout((()=>{this._init(),this._lockReconnect=!1}),this.opts?.reconnectTimeout))};_resetReconnect=()=>{this._reconnectTimer&&clearTimeout(this._reconnectTimer)};_checkHeartbeat=()=>{this._resetHeartbeat(),this._startHeartbeat()};_resetHeartbeat=()=>{this._heartbeatTimer&&clearTimeout(this._heartbeatTimer),this._pongTimer&&clearTimeout(this._pongTimer)};_startHeartbeat=()=>{this.opts.isHeartbeat&&this.opts.isReconnect&&(this._heartbeatTimer=setTimeout((()=>{this.ws&&this.ws.readyState===WebSocket.OPEN?this.ws.send(this.opts.pingMsg):this.ws?.close(),this._pongTimer=setTimeout((()=>{this.ws?.close()}),this.opts.pongTimeout)}),this.opts.pingTimeout))};sendMessage=e=>{if(this.ws){if(this.ws.readyState===WebSocket.CONNECTING){const t=setInterval((()=>{this.ws&&(this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e),this.ws.readyState!==WebSocket.CONNECTING&&clearInterval(t))}),100)}this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e)}};close=()=>{this._manualClose=!0,this.ws?.close()}}"undefined"!=typeof window&&(window.Socket=e),module.exports=e; | ||
"use strict";class e{url;opts;ws=null;_reconnectTimer=null;_manualClose=!1;_repeat=1/0;_lockReconnect=!1;_heartbeatTimer=null;_pongTimer=null;constructor(e,t){this.url=e,this.opts={isReconnect:t?.isReconnect??!0,onMessage:t?.onMessage,onError:t?.onError,close:t?.close,reconnectTimeout:t?.reconnectTimeout??300,reconnectRepeat:t?.reconnectRepeat??1/0,isHeartbeat:t?.isHeartbeat??!0,pingMsg:t?.pingMsg??"ping",pingTimeout:t?.pingTimeout??3e4,pongTimeout:t?.pongTimeout??300},this._init()}_init=()=>{this._reconnectTimer=null,this._manualClose=!1,this._repeat=0,this._lockReconnect=!1,this._heartbeatTimer=null,this._pongTimer=null,this._resetReconnect(),this._resetHeartbeat(),this._connect(),this._onopen(),this._onmessage(),this._onerror(),this._onclose()};_connect=()=>{this.ws=new WebSocket(this.url)};_onopen=()=>{this.ws&&(this.ws.onopen=()=>{this._checkHeartbeat()})};_onmessage=()=>{this.ws&&(this.ws.onmessage=e=>{this._checkHeartbeat(),"function"==typeof this.opts?.onMessage&&this.opts.onMessage(e)})};_onerror=()=>{this.ws&&(this.ws.onerror=e=>{this._reconnect(),"function"==typeof this.opts?.onError&&this.opts.onError(e)})};_onclose=()=>{this.ws&&(this.ws.onclose=e=>{this.opts.isReconnect&&!this._manualClose?this._reconnect():this.ws?.close(),"function"==typeof this.opts?.close&&this.opts.close(e)})};_reconnect=()=>{this._resetHeartbeat(),!this.ws||!this.opts.isReconnect||this._lockReconnect||Number(this.opts?.reconnectRepeat)<=this._repeat||this._manualClose||(this._lockReconnect=!0,this._repeat++,this._reconnectTimer=setTimeout((()=>{this._init(),this._lockReconnect=!1}),this.opts?.reconnectTimeout))};_resetReconnect=()=>{this._reconnectTimer&&clearTimeout(this._reconnectTimer)};_checkHeartbeat=()=>{this._resetHeartbeat(),this._startHeartbeat()};_resetHeartbeat=()=>{this._heartbeatTimer&&clearTimeout(this._heartbeatTimer),this._pongTimer&&clearTimeout(this._pongTimer)};_startHeartbeat=()=>{this.opts.isHeartbeat&&this.opts.isReconnect&&(this._heartbeatTimer=setTimeout((()=>{this.ws&&this.ws.readyState===WebSocket.OPEN?this.ws.send(this.opts.pingMsg):this.ws?.close(),this._pongTimer=setTimeout((()=>{this.ws?.close()}),this.opts.pongTimeout)}),this.opts.pingTimeout))};sendMessage=e=>{if(this.ws){if(this.ws.readyState===WebSocket.CONNECTING){const t=setInterval((()=>{this.ws&&(this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e),this.ws.readyState!==WebSocket.CONNECTING&&clearInterval(t))}),100)}this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e)}};close=()=>{this._manualClose=!0,this.ws?.close()}}"undefined"!=typeof window&&(window.Socket=e),module.exports=e; |
@@ -1,19 +0,22 @@ | ||
interface AllOptions { | ||
url: string; | ||
onMessage: (event: Event) => void; | ||
onError: (error: Event) => void; | ||
close: (event: Event) => void; | ||
isReconnect?: boolean; | ||
reconnectTimeout?: number; | ||
reconnectRepeat?: number; | ||
interface EventParams { | ||
onMessage?: (event: Event) => void; | ||
onError?: (error: Event) => void; | ||
close?: (event: Event) => void; | ||
} | ||
interface StrictVariableParams { | ||
isReconnect: boolean; | ||
reconnectTimeout: number; | ||
reconnectRepeat: number; | ||
isHeartbeat: boolean; | ||
pingMsg: string; | ||
pingMsg: string | ArrayBufferLike | Blob | ArrayBufferView; | ||
pingTimeout: number; | ||
pongTimeout: number; | ||
} | ||
interface SocketOptions extends Omit<AllOptions, "url"> { | ||
interface SocketOpts extends EventParams, StrictVariableParams { | ||
} | ||
interface SocketOptions extends EventParams, Partial<StrictVariableParams> { | ||
} | ||
declare class Socket { | ||
url: string; | ||
opts: SocketOptions; | ||
opts: SocketOpts; | ||
private ws; | ||
@@ -42,2 +45,2 @@ private _reconnectTimer; | ||
export { Socket as default }; | ||
export { SocketOptions, Socket as default }; |
@@ -1,1 +0,1 @@ | ||
class e{url;opts;ws=null;_reconnectTimer=null;_manualClose=!1;_repeat=1/0;_lockReconnect=!1;_heartbeatTimer=null;_pongTimer=null;constructor(e,t){this.url=e,this.opts={isReconnect:t?.isReconnect||!0,onMessage:t?.onMessage,onError:t?.onError,close:t?.close,reconnectTimeout:t?.reconnectTimeout||300,reconnectRepeat:t?.reconnectRepeat||1/0,isHeartbeat:t?.isHeartbeat||!0,pingMsg:t?.pingMsg||"ping",pingTimeout:t?.pingTimeout||3e4,pongTimeout:t?.pongTimeout||300},this._init()}_init=()=>{this._reconnectTimer=null,this._manualClose=!1,this._repeat=0,this._lockReconnect=!1,this._heartbeatTimer=null,this._pongTimer=null,this._resetReconnect(),this._resetHeartbeat(),this._connect(),this._onopen(),this._onmessage(),this._onerror(),this._onclose()};_connect=()=>{this.ws=new WebSocket(this.url)};_onopen=()=>{this.ws&&(this.ws.onopen=()=>{this._checkHeartbeat()})};_onmessage=()=>{this.ws&&(this.ws.onmessage=e=>{this._checkHeartbeat(),"function"==typeof this.opts.onMessage&&this.opts.onMessage(e)})};_onerror=()=>{this.ws&&(this.ws.onerror=e=>{this._reconnect(),"function"==typeof this.opts.onError&&this.opts.onError(e)})};_onclose=()=>{this.ws&&(this.ws.onclose=e=>{this.opts.isReconnect&&!this._manualClose?this._reconnect():this.ws?.close(),"function"==typeof this.opts.close&&this.opts.close(e)})};_reconnect=()=>{this._resetHeartbeat(),!this.ws||!this.opts.isReconnect||this._lockReconnect||Number(this.opts?.reconnectRepeat)<=this._repeat||this._manualClose||(this._lockReconnect=!0,this._repeat++,this._reconnectTimer=setTimeout((()=>{this._init(),this._lockReconnect=!1}),this.opts?.reconnectTimeout))};_resetReconnect=()=>{this._reconnectTimer&&clearTimeout(this._reconnectTimer)};_checkHeartbeat=()=>{this._resetHeartbeat(),this._startHeartbeat()};_resetHeartbeat=()=>{this._heartbeatTimer&&clearTimeout(this._heartbeatTimer),this._pongTimer&&clearTimeout(this._pongTimer)};_startHeartbeat=()=>{this.opts.isHeartbeat&&this.opts.isReconnect&&(this._heartbeatTimer=setTimeout((()=>{this.ws&&this.ws.readyState===WebSocket.OPEN?this.ws.send(this.opts.pingMsg):this.ws?.close(),this._pongTimer=setTimeout((()=>{this.ws?.close()}),this.opts.pongTimeout)}),this.opts.pingTimeout))};sendMessage=e=>{if(this.ws){if(this.ws.readyState===WebSocket.CONNECTING){const t=setInterval((()=>{this.ws&&(this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e),this.ws.readyState!==WebSocket.CONNECTING&&clearInterval(t))}),100)}this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e)}};close=()=>{this._manualClose=!0,this.ws?.close()}}"undefined"!=typeof window&&(window.Socket=e);export{e as default}; | ||
class e{url;opts;ws=null;_reconnectTimer=null;_manualClose=!1;_repeat=1/0;_lockReconnect=!1;_heartbeatTimer=null;_pongTimer=null;constructor(e,t){this.url=e,this.opts={isReconnect:t?.isReconnect??!0,onMessage:t?.onMessage,onError:t?.onError,close:t?.close,reconnectTimeout:t?.reconnectTimeout??300,reconnectRepeat:t?.reconnectRepeat??1/0,isHeartbeat:t?.isHeartbeat??!0,pingMsg:t?.pingMsg??"ping",pingTimeout:t?.pingTimeout??3e4,pongTimeout:t?.pongTimeout??300},this._init()}_init=()=>{this._reconnectTimer=null,this._manualClose=!1,this._repeat=0,this._lockReconnect=!1,this._heartbeatTimer=null,this._pongTimer=null,this._resetReconnect(),this._resetHeartbeat(),this._connect(),this._onopen(),this._onmessage(),this._onerror(),this._onclose()};_connect=()=>{this.ws=new WebSocket(this.url)};_onopen=()=>{this.ws&&(this.ws.onopen=()=>{this._checkHeartbeat()})};_onmessage=()=>{this.ws&&(this.ws.onmessage=e=>{this._checkHeartbeat(),"function"==typeof this.opts?.onMessage&&this.opts.onMessage(e)})};_onerror=()=>{this.ws&&(this.ws.onerror=e=>{this._reconnect(),"function"==typeof this.opts?.onError&&this.opts.onError(e)})};_onclose=()=>{this.ws&&(this.ws.onclose=e=>{this.opts.isReconnect&&!this._manualClose?this._reconnect():this.ws?.close(),"function"==typeof this.opts?.close&&this.opts.close(e)})};_reconnect=()=>{this._resetHeartbeat(),!this.ws||!this.opts.isReconnect||this._lockReconnect||Number(this.opts?.reconnectRepeat)<=this._repeat||this._manualClose||(this._lockReconnect=!0,this._repeat++,this._reconnectTimer=setTimeout((()=>{this._init(),this._lockReconnect=!1}),this.opts?.reconnectTimeout))};_resetReconnect=()=>{this._reconnectTimer&&clearTimeout(this._reconnectTimer)};_checkHeartbeat=()=>{this._resetHeartbeat(),this._startHeartbeat()};_resetHeartbeat=()=>{this._heartbeatTimer&&clearTimeout(this._heartbeatTimer),this._pongTimer&&clearTimeout(this._pongTimer)};_startHeartbeat=()=>{this.opts.isHeartbeat&&this.opts.isReconnect&&(this._heartbeatTimer=setTimeout((()=>{this.ws&&this.ws.readyState===WebSocket.OPEN?this.ws.send(this.opts.pingMsg):this.ws?.close(),this._pongTimer=setTimeout((()=>{this.ws?.close()}),this.opts.pongTimeout)}),this.opts.pingTimeout))};sendMessage=e=>{if(this.ws){if(this.ws.readyState===WebSocket.CONNECTING){const t=setInterval((()=>{this.ws&&(this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e),this.ws.readyState!==WebSocket.CONNECTING&&clearInterval(t))}),100)}this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e)}};close=()=>{this._manualClose=!0,this.ws?.close()}}"undefined"!=typeof window&&(window.Socket=e);export{e as default}; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).WebSocketWeb=t()}(this,(function(){"use strict";class e{url;opts;ws=null;_reconnectTimer=null;_manualClose=!1;_repeat=1/0;_lockReconnect=!1;_heartbeatTimer=null;_pongTimer=null;constructor(e,t){this.url=e,this.opts={isReconnect:t?.isReconnect||!0,onMessage:t?.onMessage,onError:t?.onError,close:t?.close,reconnectTimeout:t?.reconnectTimeout||300,reconnectRepeat:t?.reconnectRepeat||1/0,isHeartbeat:t?.isHeartbeat||!0,pingMsg:t?.pingMsg||"ping",pingTimeout:t?.pingTimeout||3e4,pongTimeout:t?.pongTimeout||300},this._init()}_init=()=>{this._reconnectTimer=null,this._manualClose=!1,this._repeat=0,this._lockReconnect=!1,this._heartbeatTimer=null,this._pongTimer=null,this._resetReconnect(),this._resetHeartbeat(),this._connect(),this._onopen(),this._onmessage(),this._onerror(),this._onclose()};_connect=()=>{this.ws=new WebSocket(this.url)};_onopen=()=>{this.ws&&(this.ws.onopen=()=>{this._checkHeartbeat()})};_onmessage=()=>{this.ws&&(this.ws.onmessage=e=>{this._checkHeartbeat(),"function"==typeof this.opts.onMessage&&this.opts.onMessage(e)})};_onerror=()=>{this.ws&&(this.ws.onerror=e=>{this._reconnect(),"function"==typeof this.opts.onError&&this.opts.onError(e)})};_onclose=()=>{this.ws&&(this.ws.onclose=e=>{this.opts.isReconnect&&!this._manualClose?this._reconnect():this.ws?.close(),"function"==typeof this.opts.close&&this.opts.close(e)})};_reconnect=()=>{this._resetHeartbeat(),!this.ws||!this.opts.isReconnect||this._lockReconnect||Number(this.opts?.reconnectRepeat)<=this._repeat||this._manualClose||(this._lockReconnect=!0,this._repeat++,this._reconnectTimer=setTimeout((()=>{this._init(),this._lockReconnect=!1}),this.opts?.reconnectTimeout))};_resetReconnect=()=>{this._reconnectTimer&&clearTimeout(this._reconnectTimer)};_checkHeartbeat=()=>{this._resetHeartbeat(),this._startHeartbeat()};_resetHeartbeat=()=>{this._heartbeatTimer&&clearTimeout(this._heartbeatTimer),this._pongTimer&&clearTimeout(this._pongTimer)};_startHeartbeat=()=>{this.opts.isHeartbeat&&this.opts.isReconnect&&(this._heartbeatTimer=setTimeout((()=>{this.ws&&this.ws.readyState===WebSocket.OPEN?this.ws.send(this.opts.pingMsg):this.ws?.close(),this._pongTimer=setTimeout((()=>{this.ws?.close()}),this.opts.pongTimeout)}),this.opts.pingTimeout))};sendMessage=e=>{if(this.ws){if(this.ws.readyState===WebSocket.CONNECTING){const t=setInterval((()=>{this.ws&&(this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e),this.ws.readyState!==WebSocket.CONNECTING&&clearInterval(t))}),100)}this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e)}};close=()=>{this._manualClose=!0,this.ws?.close()}}return"undefined"!=typeof window&&(window.Socket=e),e})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).WebSocketWeb=t()}(this,(function(){"use strict";class e{url;opts;ws=null;_reconnectTimer=null;_manualClose=!1;_repeat=1/0;_lockReconnect=!1;_heartbeatTimer=null;_pongTimer=null;constructor(e,t){this.url=e,this.opts={isReconnect:t?.isReconnect??!0,onMessage:t?.onMessage,onError:t?.onError,close:t?.close,reconnectTimeout:t?.reconnectTimeout??300,reconnectRepeat:t?.reconnectRepeat??1/0,isHeartbeat:t?.isHeartbeat??!0,pingMsg:t?.pingMsg??"ping",pingTimeout:t?.pingTimeout??3e4,pongTimeout:t?.pongTimeout??300},this._init()}_init=()=>{this._reconnectTimer=null,this._manualClose=!1,this._repeat=0,this._lockReconnect=!1,this._heartbeatTimer=null,this._pongTimer=null,this._resetReconnect(),this._resetHeartbeat(),this._connect(),this._onopen(),this._onmessage(),this._onerror(),this._onclose()};_connect=()=>{this.ws=new WebSocket(this.url)};_onopen=()=>{this.ws&&(this.ws.onopen=()=>{this._checkHeartbeat()})};_onmessage=()=>{this.ws&&(this.ws.onmessage=e=>{this._checkHeartbeat(),"function"==typeof this.opts?.onMessage&&this.opts.onMessage(e)})};_onerror=()=>{this.ws&&(this.ws.onerror=e=>{this._reconnect(),"function"==typeof this.opts?.onError&&this.opts.onError(e)})};_onclose=()=>{this.ws&&(this.ws.onclose=e=>{this.opts.isReconnect&&!this._manualClose?this._reconnect():this.ws?.close(),"function"==typeof this.opts?.close&&this.opts.close(e)})};_reconnect=()=>{this._resetHeartbeat(),!this.ws||!this.opts.isReconnect||this._lockReconnect||Number(this.opts?.reconnectRepeat)<=this._repeat||this._manualClose||(this._lockReconnect=!0,this._repeat++,this._reconnectTimer=setTimeout((()=>{this._init(),this._lockReconnect=!1}),this.opts?.reconnectTimeout))};_resetReconnect=()=>{this._reconnectTimer&&clearTimeout(this._reconnectTimer)};_checkHeartbeat=()=>{this._resetHeartbeat(),this._startHeartbeat()};_resetHeartbeat=()=>{this._heartbeatTimer&&clearTimeout(this._heartbeatTimer),this._pongTimer&&clearTimeout(this._pongTimer)};_startHeartbeat=()=>{this.opts.isHeartbeat&&this.opts.isReconnect&&(this._heartbeatTimer=setTimeout((()=>{this.ws&&this.ws.readyState===WebSocket.OPEN?this.ws.send(this.opts.pingMsg):this.ws?.close(),this._pongTimer=setTimeout((()=>{this.ws?.close()}),this.opts.pongTimeout)}),this.opts.pingTimeout))};sendMessage=e=>{if(this.ws){if(this.ws.readyState===WebSocket.CONNECTING){const t=setInterval((()=>{this.ws&&(this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e),this.ws.readyState!==WebSocket.CONNECTING&&clearInterval(t))}),100)}this.ws.readyState===WebSocket.OPEN&&this.ws?.send(e)}};close=()=>{this._manualClose=!0,this.ws?.close()}}return"undefined"!=typeof window&&(window.Socket=e),e})); |
{ | ||
"name": "websocket-front", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "WebSocket client for browser with reconnect and heartbeat", | ||
"main": "./dist/index.umd.js", | ||
"main": "./dist/index.esm.js", | ||
"type": "module", | ||
"types": "./dist/index.d.ts", | ||
"type": "module", | ||
"scripts": { | ||
@@ -9,0 +9,0 @@ "test": "echo \"Error: no test specified\" && exit 1", |
@@ -1,13 +0,15 @@ | ||
interface AllOptions { | ||
url: string; // websocket 地址 | ||
onMessage: (event: Event) => void; | ||
onError: (error: Event) => void; | ||
close: (event: Event) => void; | ||
interface EventParams { | ||
onMessage?: (event: Event) => void; | ||
onError?: (error: Event) => void; | ||
close?: (event: Event) => void; | ||
} | ||
interface StrictVariableParams { | ||
// 重连 | ||
isReconnect?: boolean; // 是否需要重连 | ||
reconnectTimeout?: number; // 重连间隔时间 | ||
reconnectRepeat?: number; // 重连最多执行的次数 | ||
isReconnect: boolean; // 是否需要重连 | ||
reconnectTimeout: number; // 重连间隔时间 | ||
reconnectRepeat: number; // 重连最多执行的次数 | ||
// 心跳检测 | ||
isHeartbeat: boolean; | ||
pingMsg: string; | ||
pingMsg: string | ArrayBufferLike | Blob | ArrayBufferView; | ||
pingTimeout: number; | ||
@@ -17,7 +19,9 @@ pongTimeout: number; // 多长时间没有收到返回的心跳就重启 | ||
interface SocketOptions extends Omit<AllOptions, "url"> {} | ||
interface SocketOpts extends EventParams, StrictVariableParams {} | ||
export interface SocketOptions extends EventParams, Partial<StrictVariableParams> {} | ||
class Socket { | ||
url: string; | ||
opts: SocketOptions; | ||
opts: SocketOpts; | ||
// 类里要用的变量 | ||
@@ -34,12 +38,12 @@ private ws: WebSocket | null = null; | ||
this.opts = { | ||
isReconnect: options?.isReconnect || true, | ||
isReconnect: options?.isReconnect ?? true, | ||
onMessage: options?.onMessage, | ||
onError: options?.onError, | ||
close: options?.close, | ||
reconnectTimeout: options?.reconnectTimeout || 300, // 重连的时间间隔,默认300毫秒 | ||
reconnectRepeat: options?.reconnectRepeat || Infinity, | ||
isHeartbeat: options?.isHeartbeat || true, | ||
pingMsg: options?.pingMsg || "ping", | ||
pingTimeout: options?.pingTimeout || 30000, // 发送心跳的时间间隔,默认30s | ||
pongTimeout: options?.pongTimeout || 300, | ||
reconnectTimeout: options?.reconnectTimeout ?? 300, // 重连的时间间隔,默认300毫秒 | ||
reconnectRepeat: options?.reconnectRepeat ?? Infinity, | ||
isHeartbeat: options?.isHeartbeat ?? true, | ||
pingMsg: options?.pingMsg ?? "ping", | ||
pingTimeout: options?.pingTimeout ?? 30000, // 发送心跳的时间间隔,默认30s | ||
pongTimeout: options?.pongTimeout ?? 300, | ||
}; | ||
@@ -83,3 +87,3 @@ | ||
this._checkHeartbeat(); // 如果接收到消息了,说明此前连接正常,重新检测心跳 | ||
if (typeof this.opts.onMessage !== "function") return; | ||
if (typeof this.opts?.onMessage !== "function") return; | ||
this.opts.onMessage(event); | ||
@@ -93,3 +97,3 @@ }; | ||
this._reconnect(); | ||
if (typeof this.opts.onError !== "function") return; | ||
if (typeof this.opts?.onError !== "function") return; | ||
this.opts.onError(error); | ||
@@ -107,3 +111,3 @@ }; | ||
} | ||
if (typeof this.opts.close !== "function") return; | ||
if (typeof this.opts?.close !== "function") return; | ||
this.opts.close(event); | ||
@@ -110,0 +114,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22788
256