universe-code 0.0.84 → 0.0.85
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.js +1 -0
- package/dist/socket/index.js +145 -133
- package/package.json +1 -1
package/dist/index.js
CHANGED
package/dist/socket/index.js
CHANGED
|
@@ -1,174 +1,186 @@
|
|
|
1
1
|
import { io } from "socket.io-client";
|
|
2
2
|
|
|
3
3
|
export class WebSocketService {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
socket = null;
|
|
7
|
-
config = null;
|
|
8
|
-
coreListenersBound = false;
|
|
9
|
-
lastPayloads = null;
|
|
10
|
-
lastSubscribeKey = "";
|
|
11
|
-
pendingListeners = [];
|
|
12
|
-
|
|
13
|
-
constructor() { }
|
|
14
|
-
|
|
15
|
-
static get instance() {
|
|
16
|
-
if (!WebSocketService._instance) {
|
|
17
|
-
WebSocketService._instance = new WebSocketService();
|
|
18
|
-
}
|
|
19
|
-
return WebSocketService._instance;
|
|
20
|
-
}
|
|
4
|
+
static _instance;
|
|
21
5
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"autoConnect",
|
|
29
|
-
"reconnection",
|
|
30
|
-
"reconnectionAttempts",
|
|
31
|
-
"reconnectionDelay",
|
|
32
|
-
"token",
|
|
33
|
-
];
|
|
34
|
-
|
|
35
|
-
required.forEach((key) => {
|
|
36
|
-
if (config[key] === undefined) {
|
|
37
|
-
throw new Error(`Missing required WebSocket config: ${key}`);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
6
|
+
socket = null;
|
|
7
|
+
config = null;
|
|
8
|
+
coreListenersBound = false;
|
|
9
|
+
lastPayloads = null;
|
|
10
|
+
lastSubscribeKey = "";
|
|
11
|
+
pendingListeners = [];
|
|
40
12
|
|
|
41
|
-
|
|
13
|
+
constructor() {}
|
|
42
14
|
|
|
43
|
-
|
|
15
|
+
static get instance() {
|
|
16
|
+
if (!WebSocketService._instance) {
|
|
17
|
+
WebSocketService._instance = new WebSocketService();
|
|
18
|
+
}
|
|
19
|
+
return WebSocketService._instance;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 🚨 REQUIRED strict config */
|
|
23
|
+
init(config) {
|
|
24
|
+
const required = [
|
|
25
|
+
"baseUrl",
|
|
26
|
+
"path",
|
|
27
|
+
"transports",
|
|
28
|
+
"autoConnect",
|
|
29
|
+
"reconnection",
|
|
30
|
+
"reconnectionAttempts",
|
|
31
|
+
"reconnectionDelay",
|
|
32
|
+
"auth",
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
required.forEach((key) => {
|
|
36
|
+
if (config[key] === undefined) {
|
|
37
|
+
throw new Error(`Missing required WebSocket config: ${key}`);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (!config.auth || config.auth.token === undefined) {
|
|
42
|
+
throw new Error("Missing required WebSocket auth.token");
|
|
44
43
|
}
|
|
45
44
|
|
|
46
|
-
|
|
47
|
-
setToken(token) {
|
|
48
|
-
if (!this.config) throw new Error("WebSocket not initialized");
|
|
45
|
+
this.config = config;
|
|
49
46
|
|
|
50
|
-
|
|
47
|
+
this.createSocket();
|
|
48
|
+
}
|
|
51
49
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
/** 🔑 Token update */
|
|
51
|
+
setToken(token) {
|
|
52
|
+
if (!this.config) throw new Error("WebSocket not initialized");
|
|
55
53
|
|
|
56
|
-
|
|
57
|
-
createSocket() {
|
|
58
|
-
if (this.socket) return;
|
|
59
|
-
|
|
60
|
-
if (!this.config) throw new Error("WebSocket config not initialized");
|
|
61
|
-
|
|
62
|
-
this.socket = io(this.config.baseUrl, {
|
|
63
|
-
path: this.config.path,
|
|
64
|
-
transports: this.config.transports,
|
|
65
|
-
autoConnect: this.config.autoConnect,
|
|
66
|
-
reconnection: this.config.reconnection,
|
|
67
|
-
reconnectionAttempts: this.config.reconnectionAttempts,
|
|
68
|
-
reconnectionDelay: this.config.reconnectionDelay,
|
|
69
|
-
extraHeaders: this.config.token
|
|
70
|
-
? { Authorization: `Bearer ${this.config.token}` }
|
|
71
|
-
: undefined,
|
|
72
|
-
});
|
|
54
|
+
if (!this.config.auth) this.config.auth = {};
|
|
73
55
|
|
|
74
|
-
|
|
56
|
+
this.config.auth.token = token;
|
|
75
57
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
58
|
+
if (!token) this.disconnect();
|
|
59
|
+
else this.connect();
|
|
60
|
+
}
|
|
80
61
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (!this.config.token) throw new Error("Token missing: cannot connect WebSocket");
|
|
62
|
+
/** 🚀 Create */
|
|
63
|
+
createSocket() {
|
|
64
|
+
if (this.socket) return;
|
|
85
65
|
|
|
86
|
-
|
|
66
|
+
if (!this.config) throw new Error("WebSocket config not initialized");
|
|
87
67
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
68
|
+
this.socket = io(this.config.baseUrl, {
|
|
69
|
+
path: this.config.path,
|
|
70
|
+
transports: this.config.transports,
|
|
71
|
+
autoConnect: this.config.autoConnect,
|
|
72
|
+
reconnection: this.config.reconnection,
|
|
73
|
+
reconnectionAttempts: this.config.reconnectionAttempts,
|
|
74
|
+
reconnectionDelay: this.config.reconnectionDelay,
|
|
92
75
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
76
|
+
// ⬇️ token now from auth object
|
|
77
|
+
auth:
|
|
78
|
+
this.config.auth?.token
|
|
79
|
+
? { token: this.config.auth.token }
|
|
80
|
+
: undefined,
|
|
81
|
+
});
|
|
96
82
|
|
|
97
|
-
|
|
98
|
-
this.socket.disconnect();
|
|
83
|
+
this.bindCoreListeners();
|
|
99
84
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
this.lastPayloads = null;
|
|
103
|
-
this.lastSubscribeKey = "";
|
|
85
|
+
if (this.config.autoConnect) {
|
|
86
|
+
this.connect();
|
|
104
87
|
}
|
|
88
|
+
}
|
|
105
89
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
90
|
+
/** 🔌 Connect */
|
|
91
|
+
connect() {
|
|
92
|
+
if (!this.config) throw new Error("WebSocket config not initialized");
|
|
109
93
|
|
|
110
|
-
|
|
94
|
+
if (!this.config.auth?.token) {
|
|
95
|
+
throw new Error("Token missing in auth.token: cannot connect WebSocket");
|
|
96
|
+
}
|
|
111
97
|
|
|
112
|
-
|
|
113
|
-
this.resubscribeIfNeeded();
|
|
98
|
+
if (!this.socket) this.createSocket();
|
|
114
99
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
this.socket.on(event, handler);
|
|
118
|
-
});
|
|
119
|
-
this.pendingListeners = [];
|
|
120
|
-
}
|
|
121
|
-
});
|
|
100
|
+
if (this.socket && !this.socket.connected) {
|
|
101
|
+
this.socket.connect();
|
|
122
102
|
}
|
|
103
|
+
}
|
|
123
104
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
}
|
|
105
|
+
/** ❌ Disconnect */
|
|
106
|
+
disconnect() {
|
|
107
|
+
if (!this.socket) return;
|
|
129
108
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (!this.socket) throw new Error("Socket not initialized");
|
|
109
|
+
this.socket.removeAllListeners();
|
|
110
|
+
this.socket.disconnect();
|
|
133
111
|
|
|
134
|
-
|
|
135
|
-
|
|
112
|
+
this.socket = null;
|
|
113
|
+
this.coreListenersBound = false;
|
|
114
|
+
this.lastPayloads = null;
|
|
115
|
+
this.lastSubscribeKey = "";
|
|
116
|
+
}
|
|
136
117
|
|
|
137
|
-
|
|
138
|
-
|
|
118
|
+
/** 🧠 internal */
|
|
119
|
+
bindCoreListeners() {
|
|
120
|
+
if (!this.socket || this.coreListenersBound) return;
|
|
139
121
|
|
|
140
|
-
|
|
141
|
-
this.connect();
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
122
|
+
this.coreListenersBound = true;
|
|
144
123
|
|
|
145
|
-
|
|
124
|
+
this.socket.on("connect", () => {
|
|
125
|
+
this.resubscribeIfNeeded();
|
|
126
|
+
|
|
127
|
+
if (this.pendingListeners.length) {
|
|
128
|
+
this.pendingListeners.forEach(({ event, handler }) => {
|
|
129
|
+
this.socket.on(event, handler);
|
|
130
|
+
});
|
|
131
|
+
this.pendingListeners = [];
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
resubscribeIfNeeded() {
|
|
137
|
+
if (this.lastPayloads && this.socket) {
|
|
138
|
+
this.socket.emit("subscribeMarket", this.lastPayloads);
|
|
146
139
|
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** 📩 Subscribe */
|
|
143
|
+
subscribeMarket(payload) {
|
|
144
|
+
if (!this.socket) throw new Error("Socket not initialized");
|
|
147
145
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (!this.socket) throw new Error("Socket not initialized");
|
|
146
|
+
const key = JSON.stringify(payload);
|
|
147
|
+
if (key === this.lastSubscribeKey) return;
|
|
151
148
|
|
|
152
|
-
|
|
153
|
-
|
|
149
|
+
this.lastSubscribeKey = key;
|
|
150
|
+
this.lastPayloads = payload;
|
|
154
151
|
|
|
155
|
-
|
|
156
|
-
|
|
152
|
+
if (!this.socket.connected) {
|
|
153
|
+
this.connect();
|
|
154
|
+
return;
|
|
157
155
|
}
|
|
158
156
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (!this.socket) throw new Error("Socket not initialized");
|
|
157
|
+
this.socket.emit("subscribeMarket", payload);
|
|
158
|
+
}
|
|
162
159
|
|
|
163
|
-
|
|
164
|
-
|
|
160
|
+
/** 🗑️ Unsubscribe */
|
|
161
|
+
unsubscribeMarket(payload) {
|
|
162
|
+
if (!this.socket) throw new Error("Socket not initialized");
|
|
165
163
|
|
|
166
|
-
|
|
167
|
-
|
|
164
|
+
const key = JSON.stringify(payload);
|
|
165
|
+
if (key !== this.lastSubscribeKey) return;
|
|
168
166
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
167
|
+
this.lastSubscribeKey = "";
|
|
168
|
+
this.socket.emit("unsubscribeMarket", payload);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** 👂 Listen */
|
|
172
|
+
onEvent(eventName, handler) {
|
|
173
|
+
if (!this.socket) throw new Error("Socket not initialized");
|
|
174
|
+
|
|
175
|
+
const wrapped = (data) => handler(data);
|
|
176
|
+
this.socket.on(eventName, wrapped);
|
|
177
|
+
|
|
178
|
+
return () => this.socket.off(eventName, wrapped);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** 📤 Emit */
|
|
182
|
+
sendMessage(event, data) {
|
|
183
|
+
if (!this.socket) throw new Error("Socket not initialized");
|
|
184
|
+
this.socket.emit(event, data);
|
|
185
|
+
}
|
|
174
186
|
}
|