-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathsocket-io-client.js
More file actions
39 lines (32 loc) · 808 Bytes
/
Copy pathsocket-io-client.js
File metadata and controls
39 lines (32 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const io = require("socket.io-client");
class SocketIoClient {
constructor(serverUrl, options = {}) {
this.serverUrl = serverUrl;
this.options = options; // Store options for later use
this.client = null;
}
connect() {
this.client = io(this.serverUrl, this.options); // Pass options to io()
}
disconnect() {
this.client?.disconnect();
}
waitForConnection() {
return new Promise((resolve) => {
if (this.client && this.client.connected) {
resolve();
} else {
this.client.on("connect", resolve);
}
});
}
getClient() {
return this.client;
}
sendMessage(message) {
if (this.client && this.client.connected) {
this.client.emit("message", JSON.stringify(message));
}
}
}
module.exports = SocketIoClient;