reverb-ws-client 1.0.1 → 1.0.3
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/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +39 -14
- package/dist/index.mjs +39 -14
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -49,14 +49,15 @@ var Channel = class {
|
|
|
49
49
|
auth: auth.auth,
|
|
50
50
|
...auth.channel_data ? { channel_data: auth.channel_data } : {}
|
|
51
51
|
};
|
|
52
|
-
this.client.send({
|
|
53
|
-
event: "pusher:subscribe",
|
|
54
|
-
data: {
|
|
55
|
-
channel: this.name,
|
|
56
|
-
...authPayload
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
52
|
}
|
|
53
|
+
this.client.send({
|
|
54
|
+
event: "pusher:subscribe",
|
|
55
|
+
data: {
|
|
56
|
+
channel: this.name,
|
|
57
|
+
...authPayload
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
this.subscribed = true;
|
|
60
61
|
}
|
|
61
62
|
listen(event, callback) {
|
|
62
63
|
if (!this.listeners.has(event)) {
|
|
@@ -95,11 +96,34 @@ var Channel = class {
|
|
|
95
96
|
}
|
|
96
97
|
};
|
|
97
98
|
|
|
99
|
+
// src/logger.ts
|
|
100
|
+
var Logger = class {
|
|
101
|
+
constructor(options = {}) {
|
|
102
|
+
this.options = options;
|
|
103
|
+
}
|
|
104
|
+
options;
|
|
105
|
+
log(...args) {
|
|
106
|
+
if (!this.options.debug) return;
|
|
107
|
+
console.log(...args);
|
|
108
|
+
}
|
|
109
|
+
warn(...args) {
|
|
110
|
+
if (!this.options.debug) return;
|
|
111
|
+
console.warn(...args);
|
|
112
|
+
}
|
|
113
|
+
error(...args) {
|
|
114
|
+
if (!this.options.debug) return;
|
|
115
|
+
console.error(...args);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
98
119
|
// src/ConnectionManager.ts
|
|
99
120
|
var ConnectionManager = class {
|
|
100
121
|
constructor(options, onMessage) {
|
|
101
122
|
this.options = options;
|
|
102
123
|
this.onMessage = onMessage;
|
|
124
|
+
this.logger = new Logger({
|
|
125
|
+
debug: options.debug ?? false
|
|
126
|
+
});
|
|
103
127
|
}
|
|
104
128
|
options;
|
|
105
129
|
onMessage;
|
|
@@ -109,6 +133,7 @@ var ConnectionManager = class {
|
|
|
109
133
|
maxReconnectAttempts = 5;
|
|
110
134
|
reconnectDelay = 2e3;
|
|
111
135
|
manuallyDisconnected = false;
|
|
136
|
+
logger;
|
|
112
137
|
connect() {
|
|
113
138
|
if (this.state === "connecting" || this.state === "connected") return;
|
|
114
139
|
this.manuallyDisconnected = false;
|
|
@@ -118,23 +143,23 @@ var ConnectionManager = class {
|
|
|
118
143
|
this.ws.onopen = () => {
|
|
119
144
|
this.state = "connected";
|
|
120
145
|
this.reconnectAttempts = 0;
|
|
121
|
-
|
|
146
|
+
this.logger.log("[Reverb] Connected");
|
|
122
147
|
};
|
|
123
148
|
this.ws.onmessage = (event) => {
|
|
124
149
|
try {
|
|
125
150
|
const parsed = JSON.parse(event.data);
|
|
126
151
|
this.onMessage(parsed);
|
|
127
152
|
} catch (err) {
|
|
128
|
-
|
|
153
|
+
this.logger.warn("[Reverb] WebSocket error", err);
|
|
129
154
|
}
|
|
130
155
|
};
|
|
131
156
|
this.ws.onerror = (err) => {
|
|
132
|
-
|
|
157
|
+
this.logger.warn("[Reverb] WebSocket error", err);
|
|
133
158
|
};
|
|
134
159
|
this.ws.onclose = () => {
|
|
135
160
|
this.ws = null;
|
|
136
161
|
this.state = "disconnected";
|
|
137
|
-
|
|
162
|
+
this.logger.log("[Reverb] Disconnected");
|
|
138
163
|
if (!this.manuallyDisconnected) {
|
|
139
164
|
this.reconnect();
|
|
140
165
|
}
|
|
@@ -150,7 +175,7 @@ var ConnectionManager = class {
|
|
|
150
175
|
}
|
|
151
176
|
send(data) {
|
|
152
177
|
if (!this.ws || this.state !== "connected") {
|
|
153
|
-
|
|
178
|
+
this.logger.warn("[Reverb] Cannot send, socket not connected");
|
|
154
179
|
return;
|
|
155
180
|
}
|
|
156
181
|
this.ws.send(JSON.stringify(data));
|
|
@@ -165,13 +190,13 @@ var ConnectionManager = class {
|
|
|
165
190
|
}
|
|
166
191
|
reconnect() {
|
|
167
192
|
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
168
|
-
|
|
193
|
+
this.logger.warn("[Reverb] Max reconnect attempts reached");
|
|
169
194
|
return;
|
|
170
195
|
}
|
|
171
196
|
this.state = "reconnecting";
|
|
172
197
|
this.reconnectAttempts++;
|
|
173
198
|
setTimeout(() => {
|
|
174
|
-
|
|
199
|
+
this.logger.log("[Reverb] Reconnecting...");
|
|
175
200
|
this.connect();
|
|
176
201
|
}, this.reconnectDelay);
|
|
177
202
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -23,14 +23,15 @@ var Channel = class {
|
|
|
23
23
|
auth: auth.auth,
|
|
24
24
|
...auth.channel_data ? { channel_data: auth.channel_data } : {}
|
|
25
25
|
};
|
|
26
|
-
this.client.send({
|
|
27
|
-
event: "pusher:subscribe",
|
|
28
|
-
data: {
|
|
29
|
-
channel: this.name,
|
|
30
|
-
...authPayload
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
26
|
}
|
|
27
|
+
this.client.send({
|
|
28
|
+
event: "pusher:subscribe",
|
|
29
|
+
data: {
|
|
30
|
+
channel: this.name,
|
|
31
|
+
...authPayload
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
this.subscribed = true;
|
|
34
35
|
}
|
|
35
36
|
listen(event, callback) {
|
|
36
37
|
if (!this.listeners.has(event)) {
|
|
@@ -69,11 +70,34 @@ var Channel = class {
|
|
|
69
70
|
}
|
|
70
71
|
};
|
|
71
72
|
|
|
73
|
+
// src/logger.ts
|
|
74
|
+
var Logger = class {
|
|
75
|
+
constructor(options = {}) {
|
|
76
|
+
this.options = options;
|
|
77
|
+
}
|
|
78
|
+
options;
|
|
79
|
+
log(...args) {
|
|
80
|
+
if (!this.options.debug) return;
|
|
81
|
+
console.log(...args);
|
|
82
|
+
}
|
|
83
|
+
warn(...args) {
|
|
84
|
+
if (!this.options.debug) return;
|
|
85
|
+
console.warn(...args);
|
|
86
|
+
}
|
|
87
|
+
error(...args) {
|
|
88
|
+
if (!this.options.debug) return;
|
|
89
|
+
console.error(...args);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
72
93
|
// src/ConnectionManager.ts
|
|
73
94
|
var ConnectionManager = class {
|
|
74
95
|
constructor(options, onMessage) {
|
|
75
96
|
this.options = options;
|
|
76
97
|
this.onMessage = onMessage;
|
|
98
|
+
this.logger = new Logger({
|
|
99
|
+
debug: options.debug ?? false
|
|
100
|
+
});
|
|
77
101
|
}
|
|
78
102
|
options;
|
|
79
103
|
onMessage;
|
|
@@ -83,6 +107,7 @@ var ConnectionManager = class {
|
|
|
83
107
|
maxReconnectAttempts = 5;
|
|
84
108
|
reconnectDelay = 2e3;
|
|
85
109
|
manuallyDisconnected = false;
|
|
110
|
+
logger;
|
|
86
111
|
connect() {
|
|
87
112
|
if (this.state === "connecting" || this.state === "connected") return;
|
|
88
113
|
this.manuallyDisconnected = false;
|
|
@@ -92,23 +117,23 @@ var ConnectionManager = class {
|
|
|
92
117
|
this.ws.onopen = () => {
|
|
93
118
|
this.state = "connected";
|
|
94
119
|
this.reconnectAttempts = 0;
|
|
95
|
-
|
|
120
|
+
this.logger.log("[Reverb] Connected");
|
|
96
121
|
};
|
|
97
122
|
this.ws.onmessage = (event) => {
|
|
98
123
|
try {
|
|
99
124
|
const parsed = JSON.parse(event.data);
|
|
100
125
|
this.onMessage(parsed);
|
|
101
126
|
} catch (err) {
|
|
102
|
-
|
|
127
|
+
this.logger.warn("[Reverb] WebSocket error", err);
|
|
103
128
|
}
|
|
104
129
|
};
|
|
105
130
|
this.ws.onerror = (err) => {
|
|
106
|
-
|
|
131
|
+
this.logger.warn("[Reverb] WebSocket error", err);
|
|
107
132
|
};
|
|
108
133
|
this.ws.onclose = () => {
|
|
109
134
|
this.ws = null;
|
|
110
135
|
this.state = "disconnected";
|
|
111
|
-
|
|
136
|
+
this.logger.log("[Reverb] Disconnected");
|
|
112
137
|
if (!this.manuallyDisconnected) {
|
|
113
138
|
this.reconnect();
|
|
114
139
|
}
|
|
@@ -124,7 +149,7 @@ var ConnectionManager = class {
|
|
|
124
149
|
}
|
|
125
150
|
send(data) {
|
|
126
151
|
if (!this.ws || this.state !== "connected") {
|
|
127
|
-
|
|
152
|
+
this.logger.warn("[Reverb] Cannot send, socket not connected");
|
|
128
153
|
return;
|
|
129
154
|
}
|
|
130
155
|
this.ws.send(JSON.stringify(data));
|
|
@@ -139,13 +164,13 @@ var ConnectionManager = class {
|
|
|
139
164
|
}
|
|
140
165
|
reconnect() {
|
|
141
166
|
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
142
|
-
|
|
167
|
+
this.logger.warn("[Reverb] Max reconnect attempts reached");
|
|
143
168
|
return;
|
|
144
169
|
}
|
|
145
170
|
this.state = "reconnecting";
|
|
146
171
|
this.reconnectAttempts++;
|
|
147
172
|
setTimeout(() => {
|
|
148
|
-
|
|
173
|
+
this.logger.log("[Reverb] Reconnecting...");
|
|
149
174
|
this.connect();
|
|
150
175
|
}, this.reconnectDelay);
|
|
151
176
|
}
|