@sevtech/client-websocket 0.0.1

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.
package/README.md ADDED
File without changes
@@ -0,0 +1,222 @@
1
+ import { WebSocket } from 'partysocket';
2
+ import { ErrorEvent } from 'partysocket/ws';
3
+
4
+ const pingPong = {
5
+ pingMessage: 'ping',
6
+ pingInterval: 1000,
7
+ pongMessage: 'pong',
8
+ pongTimeout: 5000,
9
+ };
10
+ const DEFAULT_OPTIONS = {
11
+ maxReconnectionDelay: 10_000,
12
+ minReconnectionDelay: 1000 + Math.random() * 4000,
13
+ minUptime: 5000,
14
+ reconnectionDelayGrowFactor: 1.3,
15
+ connectionTimeout: 4000,
16
+ maxRetries: Number.POSITIVE_INFINITY,
17
+ maxEnqueuedMessages: Number.POSITIVE_INFINITY,
18
+ startClosed: false,
19
+ };
20
+
21
+ class SocketMessage extends MessageEvent {
22
+ constructor(data) {
23
+ super('message', { data });
24
+ }
25
+ }
26
+ class PongTimeoutEvent extends Event {
27
+ constructor() {
28
+ super('pongTimeout');
29
+ this.code = 3008 /* ErrorCode.PongTimeout */;
30
+ this.reason = "[PONG TIMEOUT]" /* ErrorReason.PongTimeout */;
31
+ }
32
+ static isTimeoutEvent(event) {
33
+ if ('code' in event && 'reason' in event) {
34
+ return event.code === 3008 /* ErrorCode.PongTimeout */ && event.reason === "[PONG TIMEOUT]" /* ErrorReason.PongTimeout */;
35
+ }
36
+ return false;
37
+ }
38
+ }
39
+ class WebSocketEventTarget extends EventTarget {
40
+ // @ts-ignore
41
+ addEventListener(type, callback, options) {
42
+ super.addEventListener(type, callback, options);
43
+ }
44
+ // @ts-ignore
45
+ removeEventListener(type, callback, options) {
46
+ super.removeEventListener(type, callback, options);
47
+ }
48
+ }
49
+
50
+ class WSocket extends WebSocketEventTarget {
51
+ get _pingInterval() {
52
+ return this._options?.pingInterval ?? pingPong.pingInterval;
53
+ }
54
+ get _pongTimeout() {
55
+ return this._options?.pongTimeout ?? pingPong.pongTimeout;
56
+ }
57
+ get _pingMessage() {
58
+ if (this._options?.pingMessage instanceof Function) {
59
+ return this._options.pingMessage();
60
+ }
61
+ return this._options.pingMessage ?? pingPong.pingMessage;
62
+ }
63
+ get _shouldReconnect() {
64
+ return (this._options.maxRetries ?? DEFAULT_OPTIONS.maxRetries) > this._reconnectRetry;
65
+ }
66
+ get _isOpenState() {
67
+ return this._ws.readyState === this._ws.OPEN;
68
+ }
69
+ get readyState() {
70
+ return this._ws.readyState;
71
+ }
72
+ get url() {
73
+ return this._url;
74
+ }
75
+ constructor(url, options) {
76
+ super();
77
+ this._pingIntervalId = null;
78
+ this._pongTimeoutId = null;
79
+ this._reconnectRetry = 0;
80
+ this._options = options;
81
+ this._url = url;
82
+ this._ws = new WebSocket(this._url, options?.protocols ?? null, options ?? null);
83
+ this._ws.onopen = this._openHandler.bind(this);
84
+ this._ws.onerror = this._errorHandler.bind(this);
85
+ this._ws.onclose = this._closeHandler.bind(this);
86
+ this._ws.onmessage = this._messageHandler.bind(this);
87
+ }
88
+ send(message) {
89
+ try {
90
+ if (!this._isOpenState) {
91
+ return;
92
+ }
93
+ const serialized = this._serialize(message);
94
+ this._ws.send(serialized);
95
+ }
96
+ catch (error) {
97
+ if (error instanceof Error) {
98
+ this.dispatchEvent(new ErrorEvent(error, this));
99
+ }
100
+ }
101
+ }
102
+ close(code, reason) {
103
+ this._ws.close(code, reason);
104
+ }
105
+ reconnect(code, reason) {
106
+ this._ws.reconnect(code, reason);
107
+ }
108
+ _serialize(message) {
109
+ if (this._options.serialize instanceof Function) {
110
+ return this._options.serialize(message);
111
+ }
112
+ return typeof message !== 'string'
113
+ ? JSON.stringify(message)
114
+ : message;
115
+ }
116
+ _deserialize(message) {
117
+ if (this._options.deserialize instanceof Function) {
118
+ return this._options.deserialize(message);
119
+ }
120
+ return message;
121
+ }
122
+ _messageHandler(event) {
123
+ try {
124
+ const deserialized = this._deserialize(event.data);
125
+ if (this._isPongMessage(deserialized)) {
126
+ return this._clearPongTimeout();
127
+ }
128
+ this.dispatchEvent(new SocketMessage(deserialized));
129
+ }
130
+ catch (error) {
131
+ if (error instanceof Error) {
132
+ this.dispatchEvent(new ErrorEvent(error, this));
133
+ }
134
+ }
135
+ }
136
+ _errorHandler(event) {
137
+ this.dispatchEvent(new ErrorEvent(new Error(event.message), this));
138
+ }
139
+ _openHandler(event) {
140
+ if (this._options.pingPong) {
141
+ this._startPing();
142
+ }
143
+ this.dispatchEvent(new Event(event.type, event));
144
+ }
145
+ _closeHandler(event) {
146
+ this._clearPingInterval();
147
+ this._clearPongTimeout();
148
+ if (PongTimeoutEvent.isTimeoutEvent(event)) {
149
+ this.dispatchEvent(new PongTimeoutEvent());
150
+ }
151
+ }
152
+ _startPing() {
153
+ const ping = this._pingMessage;
154
+ if (this._isOpenState) {
155
+ this._ws.send(ping);
156
+ }
157
+ this._pingIntervalId = setInterval(() => {
158
+ this._ws.send(ping);
159
+ if (this._pongTimeoutId) {
160
+ return;
161
+ }
162
+ this._pongTimeoutId = setTimeout(() => this._reconnectHandler(), this._pongTimeout);
163
+ }, this._pingInterval);
164
+ }
165
+ _reconnectHandler() {
166
+ this._reconnectRetry += 1;
167
+ if (!this._shouldReconnect) {
168
+ return this._ws.close(3009 /* ErrorCode.ReconnectRetry */, "[RECONNECT MAX RETRY]" /* ErrorReason.ReconnectRetry */);
169
+ }
170
+ this._waitReconnect()
171
+ .then(() => this._ws.reconnect(3008 /* ErrorCode.PongTimeout */, "[PONG TIMEOUT]" /* ErrorReason.PongTimeout */));
172
+ }
173
+ _isPongMessage(message) {
174
+ if (!this._options.pingPong) {
175
+ return false;
176
+ }
177
+ if (this._options.isPongMessage instanceof Function) {
178
+ return this._options.isPongMessage(message);
179
+ }
180
+ return message === pingPong.pongMessage;
181
+ }
182
+ _clearPingInterval() {
183
+ if (!this._pingIntervalId) {
184
+ return;
185
+ }
186
+ clearInterval(this._pingIntervalId);
187
+ this._pingIntervalId = null;
188
+ }
189
+ _clearPongTimeout() {
190
+ if (!this._pongTimeoutId) {
191
+ return;
192
+ }
193
+ if (this._reconnectRetry > 0 && this._isOpenState) {
194
+ this._reconnectRetry = 0;
195
+ }
196
+ clearInterval(this._pongTimeoutId);
197
+ this._pongTimeoutId = null;
198
+ }
199
+ _getNextDelay() {
200
+ const { reconnectionDelayGrowFactor = DEFAULT_OPTIONS.reconnectionDelayGrowFactor, minReconnectionDelay = DEFAULT_OPTIONS.minReconnectionDelay, maxReconnectionDelay = DEFAULT_OPTIONS.maxReconnectionDelay } = this._options;
201
+ if (this._reconnectRetry <= 0) {
202
+ return 0;
203
+ }
204
+ return Math.min(minReconnectionDelay * reconnectionDelayGrowFactor ** (this._reconnectRetry - 1), maxReconnectionDelay);
205
+ }
206
+ _waitReconnect() {
207
+ return new Promise((resolve) => {
208
+ setTimeout(resolve, this._getNextDelay());
209
+ });
210
+ }
211
+ }
212
+
213
+ /*
214
+ * Public API Surface of client-websocket
215
+ */
216
+
217
+ /**
218
+ * Generated bundle index. Do not edit.
219
+ */
220
+
221
+ export { WSocket as ClientSocket, PongTimeoutEvent, SocketMessage };
222
+ //# sourceMappingURL=sevtech-client-websocket.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sevtech-client-websocket.mjs","sources":["../../../projects/client-websocket/src/lib/constants.ts","../../../projects/client-websocket/src/lib/events.ts","../../../projects/client-websocket/src/lib/websocket.ts","../../../projects/client-websocket/src/public-api.ts","../../../projects/client-websocket/src/sevtech-client-websocket.ts"],"sourcesContent":["import { WSocketOptions } from './types';\n\nexport const pingPong = {\n pingMessage: 'ping',\n pingInterval: 1000,\n\n pongMessage: 'pong',\n pongTimeout: 5000,\n} as const;\n\nexport const DEFAULT_OPTIONS = {\n maxReconnectionDelay: 10_000,\n minReconnectionDelay: 1000 + Math.random() * 4000,\n minUptime: 5000,\n reconnectionDelayGrowFactor: 1.3,\n connectionTimeout: 4000,\n maxRetries: Number.POSITIVE_INFINITY,\n maxEnqueuedMessages: Number.POSITIVE_INFINITY,\n startClosed: false,\n} as const satisfies WSocketOptions<unknown, unknown>;\n","import { WebSocketEventMap } from 'partysocket/ws';\nimport { ErrorCode, ErrorReason } from './types';\n\nexport class SocketMessage<T = unknown> extends MessageEvent<T> {\n constructor(data: T) {\n super('message', { data });\n }\n}\n\nexport class PongTimeoutEvent extends Event {\n readonly code = ErrorCode.PongTimeout;\n readonly reason = ErrorReason.PongTimeout;\n\n constructor() {\n super('pongTimeout');\n }\n\n static isTimeoutEvent(event: Event): boolean {\n if ('code' in event && 'reason' in event) {\n return event.code === ErrorCode.PongTimeout && event.reason === ErrorReason.PongTimeout\n }\n\n return false;\n }\n}\n\ninterface Events<Message> extends WebSocketEventMap {\n message: SocketMessage<Message>;\n pongTimeout: PongTimeoutEvent;\n}\n\nexport class WebSocketEventTarget<\n Message,\n EventMap extends Events<Message> = Events<Message>\n> extends EventTarget {\n // @ts-ignore\n override addEventListener<K extends keyof EventMap>(\n type: K & string,\n callback: (event: EventMap[K]) => void,\n options?: AddEventListenerOptions | boolean\n ): void {\n super.addEventListener(type, callback as EventListener, options);\n }\n\n // @ts-ignore\n removeEventListener<K extends keyof EventMap>(\n type: K & string,\n callback: (event: EventMap[K]) => void,\n options?: boolean | AddEventListenerOptions\n ): void {\n super.removeEventListener(type, callback as EventListener, options);\n }\n}\n","import { WebSocket as PartySocket } from 'partysocket';\nimport { ErrorEvent, Message, UrlProvider } from 'partysocket/ws';\nimport { DEFAULT_OPTIONS, pingPong } from './constants';\nimport { PongTimeoutEvent, SocketMessage, WebSocketEventTarget } from './events';\nimport { ErrorCode, ErrorReason, WSocketOptions } from './types';\n\nexport class WSocket<Send = unknown, Receive = unknown> extends WebSocketEventTarget<Receive> {\n private readonly _ws: PartySocket;\n private _pingIntervalId: ReturnType<typeof setInterval> | null = null;\n private _pongTimeoutId: ReturnType<typeof setTimeout> | null = null;\n\n private readonly _url: UrlProvider;\n private readonly _options: WSocketOptions<Send, Receive>;\n private _reconnectRetry: number = 0;\n\n private get _pingInterval(): number {\n return this._options?.pingInterval ?? pingPong.pingInterval;\n }\n\n private get _pongTimeout(): number {\n return this._options?.pongTimeout ?? pingPong.pongTimeout;\n }\n\n private get _pingMessage(): Message {\n if (this._options?.pingMessage instanceof Function) {\n return this._options.pingMessage()\n }\n\n return this._options.pingMessage ?? pingPong.pingMessage;\n }\n\n private get _shouldReconnect(): boolean {\n return (this._options.maxRetries ?? DEFAULT_OPTIONS.maxRetries) > this._reconnectRetry;\n }\n\n private get _isOpenState(): boolean {\n return this._ws.readyState === this._ws.OPEN;\n }\n\n get readyState(): number {\n return this._ws.readyState\n }\n\n get url(): UrlProvider {\n return this._url;\n }\n\n constructor(url: UrlProvider, options: WSocketOptions<Send, Receive>) {\n super()\n this._options = options;\n this._url = url;\n\n this._ws = new PartySocket(this._url, options?.protocols ?? null, options ?? null);\n this._ws.onopen = this._openHandler.bind(this);\n this._ws.onerror = this._errorHandler.bind(this);\n this._ws.onclose = this._closeHandler.bind(this);\n this._ws.onmessage = this._messageHandler.bind(this);\n }\n\n send(message: Send): void {\n try {\n if (!this._isOpenState) {\n return;\n }\n\n const serialized = this._serialize(message);\n this._ws.send(serialized);\n } catch (error) {\n if (error instanceof Error) {\n this.dispatchEvent(new ErrorEvent(error, this));\n }\n }\n }\n\n close(code?: number, reason?: string): void {\n this._ws.close(code, reason);\n }\n\n reconnect(code?: number, reason?: string): void {\n this._ws.reconnect(code, reason);\n }\n\n private _serialize(message: Send): Message {\n if (this._options.serialize instanceof Function) {\n return this._options.serialize(message);\n }\n\n return typeof message !== 'string'\n ? JSON.stringify(message)\n : message;\n }\n\n private _deserialize(message: Message): Receive {\n if (this._options.deserialize instanceof Function) {\n return this._options.deserialize(message);\n }\n\n return message as Receive;\n }\n\n private _messageHandler(event: MessageEvent<string>): void {\n try {\n const deserialized = this._deserialize(event.data);\n\n if (this._isPongMessage(deserialized)) {\n return this._clearPongTimeout();\n }\n\n this.dispatchEvent(new SocketMessage(deserialized));\n } catch (error) {\n if (error instanceof Error) {\n this.dispatchEvent(new ErrorEvent(error, this));\n }\n }\n }\n\n private _errorHandler(event: ErrorEvent): void {\n this.dispatchEvent(new ErrorEvent(new Error(event.message), this));\n }\n\n private _openHandler(event: Event): void {\n if (this._options.pingPong) {\n this._startPing();\n }\n\n this.dispatchEvent(new Event(event.type, event));\n }\n\n private _closeHandler(event: CloseEvent): void {\n this._clearPingInterval();\n this._clearPongTimeout();\n\n if (PongTimeoutEvent.isTimeoutEvent(event)) {\n this.dispatchEvent(new PongTimeoutEvent());\n }\n }\n\n private _startPing() {\n const ping = this._pingMessage;\n\n if (this._isOpenState) {\n this._ws.send(ping);\n }\n\n this._pingIntervalId = setInterval(() => {\n this._ws.send(ping);\n\n if (this._pongTimeoutId) { return; }\n\n this._pongTimeoutId = setTimeout(\n () => this._reconnectHandler(),\n this._pongTimeout\n );\n }, this._pingInterval);\n }\n\n private _reconnectHandler() {\n this._reconnectRetry += 1;\n\n if (!this._shouldReconnect) {\n return this._ws.close(ErrorCode.ReconnectRetry, ErrorReason.ReconnectRetry);\n }\n\n this._waitReconnect()\n .then(() => this._ws.reconnect(ErrorCode.PongTimeout, ErrorReason.PongTimeout))\n }\n\n private _isPongMessage(message: Receive | Message): boolean {\n if (!this._options.pingPong) { return false; }\n\n if (this._options.isPongMessage instanceof Function) {\n return this._options.isPongMessage(message);\n }\n\n return message === pingPong.pongMessage;\n }\n\n private _clearPingInterval() {\n if (!this._pingIntervalId) { return; }\n\n clearInterval(this._pingIntervalId);\n this._pingIntervalId = null;\n }\n\n private _clearPongTimeout() {\n if (!this._pongTimeoutId) { return; }\n\n if (this._reconnectRetry > 0 && this._isOpenState) {\n this._reconnectRetry = 0;\n }\n\n clearInterval(this._pongTimeoutId);\n this._pongTimeoutId = null;\n }\n\n private _getNextDelay() {\n const {\n reconnectionDelayGrowFactor = DEFAULT_OPTIONS.reconnectionDelayGrowFactor,\n minReconnectionDelay = DEFAULT_OPTIONS.minReconnectionDelay,\n maxReconnectionDelay = DEFAULT_OPTIONS.maxReconnectionDelay\n } = this._options;\n\n if (this._reconnectRetry <= 0) {\n return 0;\n }\n\n return Math.min(\n minReconnectionDelay * reconnectionDelayGrowFactor ** (this._reconnectRetry - 1),\n maxReconnectionDelay\n );\n }\n\n private _waitReconnect(): Promise<void> {\n return new Promise((resolve) => {\n setTimeout(resolve, this._getNextDelay());\n });\n }\n}\n","/*\n * Public API Surface of client-websocket\n */\n\nexport {\n WSocket as ClientSocket,\n} from './lib/websocket';\n\nexport {\n PongTimeoutEvent,\n SocketMessage,\n} from './lib/events';\n\nexport type {\n WSocketOptions as SocketOptions,\n ErrorCode as SocketErrorCodes,\n ErrorReason as SocketErrorReasons,\n} from './lib/types';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":["PartySocket"],"mappings":";;;AAEO,MAAM,QAAQ,GAAG;AACtB,IAAA,WAAW,EAAE,MAAM;AACnB,IAAA,YAAY,EAAE,IAAI;AAElB,IAAA,WAAW,EAAE,MAAM;AACnB,IAAA,WAAW,EAAE,IAAI;CACT;AAEH,MAAM,eAAe,GAAG;AAC7B,IAAA,oBAAoB,EAAE,MAAM;IAC5B,oBAAoB,EAAE,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,IAAI;AACjD,IAAA,SAAS,EAAE,IAAI;AACf,IAAA,2BAA2B,EAAE,GAAG;AAChC,IAAA,iBAAiB,EAAE,IAAI;IACvB,UAAU,EAAE,MAAM,CAAC,iBAAiB;IACpC,mBAAmB,EAAE,MAAM,CAAC,iBAAiB;AAC7C,IAAA,WAAW,EAAE,KAAK;CACiC;;AChB/C,MAAO,aAA2B,SAAQ,YAAe,CAAA;AAC7D,IAAA,WAAA,CAAY,IAAO,EAAA;AACjB,QAAA,KAAK,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,CAAC;;AAE7B;AAEK,MAAO,gBAAiB,SAAQ,KAAK,CAAA;AAIzC,IAAA,WAAA,GAAA;QACE,KAAK,CAAC,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AA,IAAI,IAAI,CAAC,eAAe,IAAI,CAAC,EAAE;AAC7B,YAAA,OAAO,CAAC;;AAGV,QAAA,OAAO,IAAI,CAAC,GAAG,CACb,oBAAoB,GAAG,2BAA2B,KAAK,IAAI,CAAC,eAAe,GAAG,CAAC,CAAC,EAChF,oBAAoB,CACrB;;IAGK,cAAc,GAAA;AACpB,QAAA,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,KAAI;YAC7B,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC;AAC3C,SAAC,CAAC;;AAEL;;ACzND;;AAEG;;ACFH;;AAEG;;;;"}
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Generated bundle index. Do not edit.
3
+ */
4
+ /// <amd-module name="@sevtech/client-websocket" />
5
+ export * from './public-api';
@@ -0,0 +1,16 @@
1
+ export declare const pingPong: {
2
+ readonly pingMessage: "ping";
3
+ readonly pingInterval: 1000;
4
+ readonly pongMessage: "pong";
5
+ readonly pongTimeout: 5000;
6
+ };
7
+ export declare const DEFAULT_OPTIONS: {
8
+ readonly maxReconnectionDelay: 10000;
9
+ readonly minReconnectionDelay: number;
10
+ readonly minUptime: 5000;
11
+ readonly reconnectionDelayGrowFactor: 1.3;
12
+ readonly connectionTimeout: 4000;
13
+ readonly maxRetries: number;
14
+ readonly maxEnqueuedMessages: number;
15
+ readonly startClosed: false;
16
+ };
@@ -0,0 +1,20 @@
1
+ import { WebSocketEventMap } from 'partysocket/ws';
2
+ import { ErrorCode, ErrorReason } from './types';
3
+ export declare class SocketMessage<T = unknown> extends MessageEvent<T> {
4
+ constructor(data: T);
5
+ }
6
+ export declare class PongTimeoutEvent extends Event {
7
+ readonly code = ErrorCode.PongTimeout;
8
+ readonly reason = ErrorReason.PongTimeout;
9
+ constructor();
10
+ static isTimeoutEvent(event: Event): boolean;
11
+ }
12
+ interface Events<Message> extends WebSocketEventMap {
13
+ message: SocketMessage<Message>;
14
+ pongTimeout: PongTimeoutEvent;
15
+ }
16
+ export declare class WebSocketEventTarget<Message, EventMap extends Events<Message> = Events<Message>> extends EventTarget {
17
+ addEventListener<K extends keyof EventMap>(type: K & string, callback: (event: EventMap[K]) => void, options?: AddEventListenerOptions | boolean): void;
18
+ removeEventListener<K extends keyof EventMap>(type: K & string, callback: (event: EventMap[K]) => void, options?: boolean | AddEventListenerOptions): void;
19
+ }
20
+ export {};
package/lib/types.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { Message, Options, ProtocolsProvider } from 'partysocket/ws';
2
+ export declare const enum ErrorCode {
3
+ PongTimeout = 3008,
4
+ ReconnectRetry = 3009
5
+ }
6
+ export declare const enum ErrorReason {
7
+ PongTimeout = "[PONG TIMEOUT]",
8
+ ReconnectRetry = "[RECONNECT MAX RETRY]"
9
+ }
10
+ type PongFunction<Message> = (message: Message) => boolean;
11
+ type OmitOptions = Omit<Options, 'WebSocket' | 'debugLogger' | 'debug'>;
12
+ export interface WSocketOptions<Send, Receive> extends OmitOptions {
13
+ protocols?: ProtocolsProvider;
14
+ pingPong?: boolean;
15
+ pingInterval?: number;
16
+ pongTimeout?: number;
17
+ pingMessage?: (() => Message) | Message;
18
+ isPongMessage?: PongFunction<Receive | Message>;
19
+ serialize?: (value: Send) => Message;
20
+ deserialize?: (value: Message) => Receive;
21
+ }
22
+ export {};
@@ -0,0 +1,35 @@
1
+ import { UrlProvider } from 'partysocket/ws';
2
+ import { WebSocketEventTarget } from './events';
3
+ import { WSocketOptions } from './types';
4
+ export declare class WSocket<Send = unknown, Receive = unknown> extends WebSocketEventTarget<Receive> {
5
+ private readonly _ws;
6
+ private _pingIntervalId;
7
+ private _pongTimeoutId;
8
+ private readonly _url;
9
+ private readonly _options;
10
+ private _reconnectRetry;
11
+ private get _pingInterval();
12
+ private get _pongTimeout();
13
+ private get _pingMessage();
14
+ private get _shouldReconnect();
15
+ private get _isOpenState();
16
+ get readyState(): number;
17
+ get url(): UrlProvider;
18
+ constructor(url: UrlProvider, options: WSocketOptions<Send, Receive>);
19
+ send(message: Send): void;
20
+ close(code?: number, reason?: string): void;
21
+ reconnect(code?: number, reason?: string): void;
22
+ private _serialize;
23
+ private _deserialize;
24
+ private _messageHandler;
25
+ private _errorHandler;
26
+ private _openHandler;
27
+ private _closeHandler;
28
+ private _startPing;
29
+ private _reconnectHandler;
30
+ private _isPongMessage;
31
+ private _clearPingInterval;
32
+ private _clearPongTimeout;
33
+ private _getNextDelay;
34
+ private _waitReconnect;
35
+ }
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@sevtech/client-websocket",
3
+ "version": "0.0.1",
4
+ "peerDependencies": {
5
+ "partysocket": "^1.1.3"
6
+ },
7
+ "dependencies": {
8
+ "tslib": "^2.3.0"
9
+ },
10
+ "sideEffects": false,
11
+ "module": "fesm2022/sevtech-client-websocket.mjs",
12
+ "typings": "index.d.ts",
13
+ "exports": {
14
+ "./package.json": {
15
+ "default": "./package.json"
16
+ },
17
+ ".": {
18
+ "types": "./index.d.ts",
19
+ "default": "./fesm2022/sevtech-client-websocket.mjs"
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,3 @@
1
+ export { WSocket as ClientSocket, } from './lib/websocket';
2
+ export { PongTimeoutEvent, SocketMessage, } from './lib/events';
3
+ export type { WSocketOptions as SocketOptions, ErrorCode as SocketErrorCodes, ErrorReason as SocketErrorReasons, } from './lib/types';