@africode/core 5.0.10 → 5.0.13
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/AGENTS.md +585 -585
- package/AGENT_INSTRUCTIONS.md +595 -595
- package/COMPONENT_SCHEMA.json +1826 -1800
- package/LICENSE +623 -623
- package/README.md +476 -476
- package/bin/africode.js +73 -73
- package/bin/cli.ts +82 -82
- package/bin/create-africode.js +220 -220
- package/bin/scaffold.ts +218 -218
- package/components/accordion.js +183 -183
- package/components/alert.js +131 -131
- package/components/auth-form.js +154 -154
- package/components/auth.js +172 -172
- package/components/avatar.js +117 -117
- package/components/badge.js +104 -104
- package/components/base.d.ts +139 -139
- package/components/base.js +234 -234
- package/components/breadcrumb.js +76 -72
- package/components/button.js +164 -164
- package/components/card.js +144 -137
- package/components/carousel.js +32 -32
- package/components/cart-drawer.js +28 -28
- package/components/code-block.js +20 -20
- package/components/contact-form.js +25 -25
- package/components/cookie-consent.js +23 -23
- package/components/cta-banner.js +38 -38
- package/components/cultural-card.js +243 -243
- package/components/dashboard-activity-list.js +103 -103
- package/components/dashboard-card.js +85 -85
- package/components/dashboard-metric.js +81 -81
- package/components/dashboard-shell.js +85 -85
- package/components/dashboard-topbar.js +84 -84
- package/components/divider.js +83 -83
- package/components/dropdown.js +171 -171
- package/components/error-boundary.js +155 -155
- package/components/faq-accordion.js +23 -23
- package/components/feature-grid.js +85 -85
- package/components/file-uploader.js +22 -22
- package/components/filter-bar.js +19 -19
- package/components/footer.js +113 -113
- package/components/form.js +131 -131
- package/components/gallery.js +22 -22
- package/components/hero.js +140 -138
- package/components/icon.js +36 -36
- package/components/index.js +128 -128
- package/components/input.js +252 -256
- package/components/kanga-card.js +185 -185
- package/components/kyc-upload.js +173 -173
- package/components/language-switcher.js +108 -108
- package/components/loader.js +80 -80
- package/components/modal.js +262 -262
- package/components/motion.js +84 -84
- package/components/nav-drawer.js +216 -216
- package/components/navbar.js +243 -236
- package/components/newsletter-signup.js +24 -24
- package/components/pagination.js +31 -31
- package/components/pattern-showcase.js +225 -225
- package/components/pricing-card.js +100 -100
- package/components/product-card.js +41 -41
- package/components/progress.js +132 -134
- package/components/react.js +111 -111
- package/components/search-box.js +24 -24
- package/components/section.js +54 -54
- package/components/select.js +321 -322
- package/components/sidebar.js +180 -180
- package/components/simple-chart.js +22 -22
- package/components/skeleton.js +85 -85
- package/components/stepper.js +28 -28
- package/components/table.js +181 -181
- package/components/tabs.js +202 -202
- package/components/team-grid.js +23 -23
- package/components/testimonial.js +94 -94
- package/components/theme-toggle.js +82 -82
- package/components/toast.js +139 -139
- package/components/tooltip.js +167 -167
- package/components/transaction-ledger.js +138 -138
- package/components/ui-avatar.js +79 -79
- package/components/ui-badge.js +61 -61
- package/components/ui-button.js +149 -149
- package/components/ui-card.js +74 -75
- package/components/ui-chip.js +87 -87
- package/components/ui-input.js +110 -110
- package/components/ui-popover.js +84 -84
- package/components/ui-progress-ring.js +60 -60
- package/components/ui-select.js +85 -85
- package/components/ui-stat.js +75 -75
- package/components/ui-switch.js +85 -85
- package/components/ui-table.js +69 -69
- package/components/ui-tabs.js +99 -99
- package/components/ui-tag.js +64 -64
- package/components/ui-textarea.js +84 -84
- package/components/ui-tooltip.js +66 -66
- package/components/video-player.js +22 -22
- package/components/wallet-balance.js +114 -114
- package/core/a2ui-schema-manager.js +351 -351
- package/core/a2ui.js +696 -696
- package/core/actions.js +27 -27
- package/core/bun-runtime.js +398 -397
- package/core/cli/commands/add.js +23 -23
- package/core/cli/commands/audit.js +58 -58
- package/core/cli/commands/build.js +193 -193
- package/core/cli/commands/create-plugin.js +241 -241
- package/core/cli/commands/dev.js +252 -285
- package/core/cli/commands/lint.js +23 -23
- package/core/cli/commands/test.js +33 -34
- package/core/cli/migrator.js +71 -71
- package/core/cli/ui.js +56 -56
- package/core/compliance.js +823 -823
- package/core/config.js +265 -265
- package/core/db-advanced.js +478 -481
- package/core/db.js +282 -284
- package/core/enhanced-hmr.js +406 -406
- package/core/errors.js +222 -222
- package/core/file-router.js +52 -50
- package/core/hmr-client.js +203 -204
- package/core/hmr.js +205 -197
- package/core/html.d.ts +30 -30
- package/core/html.js +84 -84
- package/core/hydration.js +52 -52
- package/core/lipa-namba-journey.js +548 -605
- package/core/logging.js +14 -14
- package/core/middleware.js +145 -145
- package/core/motion.js +106 -106
- package/core/nida-cig-middleware.js +417 -460
- package/core/patterns.d.ts +124 -124
- package/core/patterns.js +947 -1001
- package/core/plugins/index.js +345 -345
- package/core/request-analytics.js +253 -253
- package/core/request-identity.js +93 -93
- package/core/router.js +386 -386
- package/core/sdk-browser.js +140 -140
- package/core/sdk-client.js +61 -61
- package/core/sdk.d.ts +133 -133
- package/core/sdk.js +148 -148
- package/core/seo.js +76 -76
- package/core/server/auth-endpoints.js +339 -339
- package/core/server/auth.js +180 -180
- package/core/server/csrf.js +200 -206
- package/core/server/db.js +39 -39
- package/core/server/middleware.js +324 -324
- package/core/server/rate-limit.js +238 -238
- package/core/server/render.js +67 -69
- package/core/server/router.js +102 -120
- package/core/session-store.js +68 -68
- package/core/shim.js +28 -28
- package/core/state.d.ts +86 -86
- package/core/state.js +276 -276
- package/core/store.d.ts +122 -122
- package/core/store.js +61 -61
- package/core/validation.d.ts +232 -232
- package/core/validation.js +602 -602
- package/core/websocket.js +656 -641
- package/dist/africode.js +233 -230
- package/dist/africode.js.map +83 -83
- package/dist/build-info.json +3 -3
- package/dist/components.js +195 -192
- package/dist/components.js.map +73 -73
- package/dist/styles/africanity.css +321 -321
- package/dist/styles/typography.css +140 -140
- package/docs/IDE-Guide.md +50 -50
- package/package.json +113 -113
- package/scripts/generate-component-schema.js +82 -79
- package/styles/africanity.css +321 -321
- package/styles/typography.css +140 -140
- package/templates/starter/.env.example +15 -15
- package/templates/starter/africode.config.js +40 -40
- package/templates/starter/package.json +13 -13
- package/templates/starter/src/index.js +18 -18
- package/templates/starter/src/pages/index.html +55 -55
- package/templates/starter/src/pages/index.js +17 -17
- package/templates/starter/src/styles/main.css +3 -3
- package/templates/starter-3d/.env.example +7 -7
- package/templates/starter-3d/africode.config.js +29 -29
- package/templates/starter-3d/components/af-model-viewer.js +125 -125
- package/templates/starter-3d/package.json +14 -14
- package/templates/starter-3d/src/pages/index.html +55 -55
- package/templates/starter-3d/src/pages/index.js +50 -50
- package/templates/starter-3d/src/styles/main.css +3 -3
- package/templates/starter-dashboard/.env.example +21 -21
- package/templates/starter-dashboard/africode.config.js +20 -20
- package/templates/starter-dashboard/package.json +14 -14
- package/templates/starter-dashboard/src/index.js +17 -17
- package/templates/starter-dashboard/src/pages/api/analytics.js +24 -24
- package/templates/starter-dashboard/src/pages/index.html +118 -118
- package/templates/starter-dashboard/src/pages/index.js +110 -110
- package/templates/starter-dashboard/src/styles/main.css +172 -172
- package/templates/starter-fintech/.env.example +28 -28
- package/templates/starter-fintech/africode.config.js +20 -20
- package/templates/starter-fintech/package.json +15 -15
- package/templates/starter-fintech/src/index.js +17 -17
- package/templates/starter-fintech/src/pages/api/auth.js +45 -45
- package/templates/starter-fintech/src/pages/api/dashboard.js +19 -0
- package/templates/starter-fintech/src/pages/api/transfer.js +65 -65
- package/templates/starter-fintech/src/pages/api/wallet.js +39 -39
- package/templates/starter-fintech/src/pages/api/webhooks/payment.js +32 -32
- package/templates/starter-fintech/src/pages/index.html +169 -169
- package/templates/starter-fintech/src/pages/index.js +161 -161
- package/templates/starter-fintech/src/styles/main.css +246 -246
- package/templates/starter-react/.env.example +15 -15
- package/templates/starter-react/africode.config.js +40 -40
- package/templates/starter-react/package.json +15 -15
- package/templates/starter-react/src/pages/index.html +55 -55
- package/templates/starter-react/src/pages/index.js +68 -68
- package/templates/starter-react/src/styles/main.css +3 -3
- package/templates/starter-tailwind/.env.example +15 -15
- package/templates/starter-tailwind/africode.config.js +40 -40
- package/templates/starter-tailwind/package.json +19 -19
- package/templates/starter-tailwind/src/pages/index.html +55 -55
- package/templates/starter-tailwind/src/pages/index.js +67 -67
- package/templates/starter-tailwind/src/styles/main.css +3 -3
- package/templates/starter-tailwind/src/styles/tailwind.css +1 -1
- package/templates/starter-tailwind/src/tailwind-loader.js +30 -30
- package/templates/starter-tailwind/tailwind.config.cjs +18 -18
- package/templates/starter-website/.env.example +18 -18
- package/templates/starter-website/africode.config.js +20 -20
- package/templates/starter-website/package.json +14 -14
- package/templates/starter-website/src/index.js +17 -17
- package/templates/starter-website/src/pages/index.html +124 -124
- package/templates/starter-website/src/pages/index.js +116 -116
- package/templates/starter-website/src/styles/main.css +195 -195
- package/bin/africode.js.1758507140 +0 -343
- package/core/heartbeat.js +0 -64
package/core/websocket.js
CHANGED
|
@@ -1,641 +1,656 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AfriCode WebSocket Support
|
|
3
|
-
*
|
|
4
|
-
* Real-time bidirectional communication with:
|
|
5
|
-
* - Persistent WebSocket connections
|
|
6
|
-
* - Room/channel management
|
|
7
|
-
* - Event broadcasting
|
|
8
|
-
* - Message queueing
|
|
9
|
-
* - Presence tracking
|
|
10
|
-
* - Automatic reconnection
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { frameworkLog } from './logging.js';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* WebSocket Server Manager
|
|
17
|
-
* Handles connections, rooms, and broadcasting
|
|
18
|
-
*/
|
|
19
|
-
export class WebSocketServer {
|
|
20
|
-
constructor() {
|
|
21
|
-
this.connections = new Map(); // connection_id -> WebSocket
|
|
22
|
-
this.rooms = new Map(); // room_name -> Set of connection_ids
|
|
23
|
-
this.messageHandlers = new Map(); // event_type -> handler function
|
|
24
|
-
this.userConnections = new Map(); // user_id -> Set of connection_ids
|
|
25
|
-
this.messageQueue = []; // For offline message queueing
|
|
26
|
-
this.maxQueueSize = 1000;
|
|
27
|
-
this.connectionCounter = 0;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
this.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
this.
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
this.
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
this.
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
this.
|
|
489
|
-
this.
|
|
490
|
-
this.
|
|
491
|
-
this.
|
|
492
|
-
this.
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
this.
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
this.
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
1
|
+
/**
|
|
2
|
+
* AfriCode WebSocket Support
|
|
3
|
+
*
|
|
4
|
+
* Real-time bidirectional communication with:
|
|
5
|
+
* - Persistent WebSocket connections
|
|
6
|
+
* - Room/channel management
|
|
7
|
+
* - Event broadcasting
|
|
8
|
+
* - Message queueing
|
|
9
|
+
* - Presence tracking
|
|
10
|
+
* - Automatic reconnection
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { frameworkLog } from './logging.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* WebSocket Server Manager
|
|
17
|
+
* Handles connections, rooms, and broadcasting
|
|
18
|
+
*/
|
|
19
|
+
export class WebSocketServer {
|
|
20
|
+
constructor() {
|
|
21
|
+
this.connections = new Map(); // connection_id -> WebSocket
|
|
22
|
+
this.rooms = new Map(); // room_name -> Set of connection_ids
|
|
23
|
+
this.messageHandlers = new Map(); // event_type -> handler function
|
|
24
|
+
this.userConnections = new Map(); // user_id -> Set of connection_ids
|
|
25
|
+
this.messageQueue = []; // For offline message queueing
|
|
26
|
+
this.maxQueueSize = 1000;
|
|
27
|
+
this.connectionCounter = 0;
|
|
28
|
+
this.heartbeatIntervals = new Map(); // connection_id -> interval_id
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Register a message handler for a specific event type
|
|
33
|
+
*/
|
|
34
|
+
on(eventType, handler) {
|
|
35
|
+
if (!this.messageHandlers.has(eventType)) {
|
|
36
|
+
this.messageHandlers.set(eventType, []);
|
|
37
|
+
}
|
|
38
|
+
this.messageHandlers.get(eventType).push(handler);
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Handle new WebSocket connection
|
|
44
|
+
*/
|
|
45
|
+
handleConnection(ws, userId = null) {
|
|
46
|
+
const connectionId = `conn_${++this.connectionCounter}`;
|
|
47
|
+
this.connections.set(connectionId, {
|
|
48
|
+
ws,
|
|
49
|
+
userId,
|
|
50
|
+
connectedAt: Date.now(),
|
|
51
|
+
lastHeartbeat: Date.now(),
|
|
52
|
+
rooms: new Set()
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
if (userId) {
|
|
56
|
+
if (!this.userConnections.has(userId)) {
|
|
57
|
+
this.userConnections.set(userId, new Set());
|
|
58
|
+
}
|
|
59
|
+
this.userConnections.get(userId).add(connectionId);
|
|
60
|
+
this.broadcast('user:online', { userId, connectionId }, null, 'presence');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Setup message handler
|
|
64
|
+
ws.onmessage = (event) => this.handleMessage(connectionId, event);
|
|
65
|
+
|
|
66
|
+
// Setup close handler
|
|
67
|
+
ws.onclose = () => this.handleDisconnection(connectionId);
|
|
68
|
+
|
|
69
|
+
// Send welcome message
|
|
70
|
+
this.send(connectionId, 'connection:established', {
|
|
71
|
+
connectionId,
|
|
72
|
+
userId,
|
|
73
|
+
serverTime: Date.now()
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Start heartbeat
|
|
77
|
+
const interval = this.startHeartbeat(connectionId);
|
|
78
|
+
this.heartbeatIntervals.set(connectionId, interval);
|
|
79
|
+
|
|
80
|
+
return connectionId;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Handle incoming messages
|
|
85
|
+
*/
|
|
86
|
+
handleMessage(connectionId, event) {
|
|
87
|
+
try {
|
|
88
|
+
const data = JSON.parse(event.data);
|
|
89
|
+
const { type, payload, roomId } = data;
|
|
90
|
+
|
|
91
|
+
const connInfo = this.connections.get(connectionId);
|
|
92
|
+
if (!connInfo) {return;}
|
|
93
|
+
|
|
94
|
+
connInfo.lastHeartbeat = Date.now();
|
|
95
|
+
|
|
96
|
+
// Handle special system messages
|
|
97
|
+
if (type === 'ping') {
|
|
98
|
+
this.send(connectionId, 'pong', { timestamp: Date.now() });
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (type === 'join:room') {
|
|
103
|
+
this.joinRoom(connectionId, payload.roomId, payload.userId);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (type === 'leave:room') {
|
|
108
|
+
this.leaveRoom(connectionId, payload.roomId);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Call registered handlers
|
|
113
|
+
const handlers = this.messageHandlers.get(type) || [];
|
|
114
|
+
for (const handler of handlers) {
|
|
115
|
+
try {
|
|
116
|
+
handler({
|
|
117
|
+
connectionId,
|
|
118
|
+
userId: connInfo.userId,
|
|
119
|
+
payload,
|
|
120
|
+
roomId,
|
|
121
|
+
timestamp: Date.now()
|
|
122
|
+
});
|
|
123
|
+
} catch (error) {
|
|
124
|
+
frameworkLog('error', `Error in handler for ${type}:`, error);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
} catch (error) {
|
|
128
|
+
frameworkLog('error', 'Error handling message:', error);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Handle disconnection
|
|
134
|
+
*/
|
|
135
|
+
handleDisconnection(connectionId) {
|
|
136
|
+
const connInfo = this.connections.get(connectionId);
|
|
137
|
+
if (!connInfo) {return;}
|
|
138
|
+
|
|
139
|
+
const { userId } = connInfo;
|
|
140
|
+
|
|
141
|
+
// Clear heartbeat interval
|
|
142
|
+
const hbInterval = this.heartbeatIntervals.get(connectionId);
|
|
143
|
+
if (hbInterval) {
|
|
144
|
+
clearInterval(hbInterval);
|
|
145
|
+
this.heartbeatIntervals.delete(connectionId);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Remove from all rooms
|
|
149
|
+
for (const roomId of connInfo.rooms) {
|
|
150
|
+
this.leaveRoom(connectionId, roomId);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Remove from user connections
|
|
154
|
+
if (userId) {
|
|
155
|
+
const userConns = this.userConnections.get(userId);
|
|
156
|
+
if (userConns) {
|
|
157
|
+
userConns.delete(connectionId);
|
|
158
|
+
if (userConns.size === 0) {
|
|
159
|
+
this.userConnections.delete(userId);
|
|
160
|
+
this.broadcast('user:offline', { userId }, null, 'presence');
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this.connections.delete(connectionId);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Join a room
|
|
170
|
+
*/
|
|
171
|
+
joinRoom(connectionId, roomId, userId = null) {
|
|
172
|
+
if (!this.rooms.has(roomId)) {
|
|
173
|
+
this.rooms.set(roomId, new Set());
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const room = this.rooms.get(roomId);
|
|
177
|
+
room.add(connectionId);
|
|
178
|
+
|
|
179
|
+
const connInfo = this.connections.get(connectionId);
|
|
180
|
+
if (connInfo) {
|
|
181
|
+
connInfo.rooms.add(roomId);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Broadcast user joined to room
|
|
185
|
+
this.broadcast(
|
|
186
|
+
'room:user:joined',
|
|
187
|
+
{ userId, connectionId, roomId },
|
|
188
|
+
roomId,
|
|
189
|
+
'room'
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
// Send room members list to new member
|
|
193
|
+
const members = Array.from(room).map(cid => {
|
|
194
|
+
const ci = this.connections.get(cid);
|
|
195
|
+
return { connectionId: cid, userId: ci?.userId };
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
this.send(connectionId, 'room:members', { roomId, members });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Leave a room
|
|
203
|
+
*/
|
|
204
|
+
leaveRoom(connectionId, roomId) {
|
|
205
|
+
const room = this.rooms.get(roomId);
|
|
206
|
+
if (!room) {return;}
|
|
207
|
+
|
|
208
|
+
room.delete(connectionId);
|
|
209
|
+
|
|
210
|
+
const connInfo = this.connections.get(connectionId);
|
|
211
|
+
if (connInfo) {
|
|
212
|
+
connInfo.rooms.delete(roomId);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Broadcast user left to room
|
|
216
|
+
this.broadcast(
|
|
217
|
+
'room:user:left',
|
|
218
|
+
{ connectionId, roomId },
|
|
219
|
+
roomId,
|
|
220
|
+
'room'
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
// Clean up empty rooms
|
|
224
|
+
if (room.size === 0) {
|
|
225
|
+
this.rooms.delete(roomId);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Send message to specific connection
|
|
231
|
+
*/
|
|
232
|
+
send(connectionId, type, payload) {
|
|
233
|
+
const connInfo = this.connections.get(connectionId);
|
|
234
|
+
if (!connInfo) {return false;}
|
|
235
|
+
|
|
236
|
+
try {
|
|
237
|
+
connInfo.ws.send(JSON.stringify({
|
|
238
|
+
type,
|
|
239
|
+
payload,
|
|
240
|
+
timestamp: Date.now()
|
|
241
|
+
}));
|
|
242
|
+
return true;
|
|
243
|
+
} catch (error) {
|
|
244
|
+
frameworkLog('error', `Error sending to ${connectionId}:`, error);
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Broadcast message to all connections in a room or globally
|
|
251
|
+
*/
|
|
252
|
+
broadcast(type, payload, roomId = null, scope = 'global') {
|
|
253
|
+
const timestamp = Date.now();
|
|
254
|
+
|
|
255
|
+
if (scope === 'room' && roomId) {
|
|
256
|
+
const room = this.rooms.get(roomId);
|
|
257
|
+
if (room) {
|
|
258
|
+
for (const connectionId of room) {
|
|
259
|
+
const connInfo = this.connections.get(connectionId);
|
|
260
|
+
if (connInfo) {
|
|
261
|
+
try {
|
|
262
|
+
connInfo.ws.send(JSON.stringify({
|
|
263
|
+
type,
|
|
264
|
+
payload,
|
|
265
|
+
roomId,
|
|
266
|
+
timestamp
|
|
267
|
+
}));
|
|
268
|
+
} catch (error) {
|
|
269
|
+
frameworkLog('error', `Error broadcasting to ${connectionId}:`, error);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
} else if (scope === 'user' && payload.userId) {
|
|
275
|
+
const userConns = this.userConnections.get(payload.userId);
|
|
276
|
+
if (userConns) {
|
|
277
|
+
for (const connectionId of userConns) {
|
|
278
|
+
this.send(connectionId, type, payload);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
} else if (scope === 'presence') {
|
|
282
|
+
// Broadcast presence to all connections
|
|
283
|
+
for (const [connId, connInfo] of this.connections.entries()) {
|
|
284
|
+
try {
|
|
285
|
+
connInfo.ws.send(JSON.stringify({
|
|
286
|
+
type,
|
|
287
|
+
payload,
|
|
288
|
+
timestamp
|
|
289
|
+
}));
|
|
290
|
+
} catch (error) {
|
|
291
|
+
frameworkLog('error', `Error broadcasting presence to ${connId}:`, error);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
} else {
|
|
295
|
+
// Global broadcast
|
|
296
|
+
for (const [connId, connInfo] of this.connections.entries()) {
|
|
297
|
+
try {
|
|
298
|
+
connInfo.ws.send(JSON.stringify({
|
|
299
|
+
type,
|
|
300
|
+
payload,
|
|
301
|
+
timestamp
|
|
302
|
+
}));
|
|
303
|
+
} catch (error) {
|
|
304
|
+
frameworkLog('error', `Error broadcasting to ${connId}:`, error);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Send message to all users in a room
|
|
312
|
+
*/
|
|
313
|
+
toRoom(roomId, type, payload, excludeConnectionId = null) {
|
|
314
|
+
const room = this.rooms.get(roomId);
|
|
315
|
+
if (!room) {return;}
|
|
316
|
+
|
|
317
|
+
for (const connectionId of room) {
|
|
318
|
+
if (excludeConnectionId === connectionId) {continue;}
|
|
319
|
+
this.send(connectionId, type, payload);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Send message to specific user (all their connections)
|
|
325
|
+
*/
|
|
326
|
+
toUser(userId, type, payload) {
|
|
327
|
+
const userConns = this.userConnections.get(userId);
|
|
328
|
+
if (!userConns) {return;}
|
|
329
|
+
|
|
330
|
+
for (const connectionId of userConns) {
|
|
331
|
+
this.send(connectionId, type, payload);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Queue message for offline user
|
|
337
|
+
*/
|
|
338
|
+
queueMessage(userId, type, payload) {
|
|
339
|
+
if (this.messageQueue.length >= this.maxQueueSize) {
|
|
340
|
+
this.messageQueue.shift(); // Remove oldest
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
this.messageQueue.push({
|
|
344
|
+
userId,
|
|
345
|
+
type,
|
|
346
|
+
payload,
|
|
347
|
+
queuedAt: Date.now()
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Deliver queued messages to user
|
|
353
|
+
*/
|
|
354
|
+
deliverQueuedMessages(userId) {
|
|
355
|
+
const queued = this.messageQueue.filter(msg => msg.userId === userId);
|
|
356
|
+
|
|
357
|
+
for (const msg of queued) {
|
|
358
|
+
this.toUser(userId, msg.type, msg.payload);
|
|
359
|
+
this.messageQueue = this.messageQueue.filter(m => m !== msg);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return queued.length;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Start heartbeat for connection
|
|
367
|
+
*/
|
|
368
|
+
startHeartbeat(connectionId) {
|
|
369
|
+
const interval = setInterval(() => {
|
|
370
|
+
const connInfo = this.connections.get(connectionId);
|
|
371
|
+
if (!connInfo) {
|
|
372
|
+
clearInterval(interval);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const timeSinceLastHeartbeat = Date.now() - connInfo.lastHeartbeat;
|
|
377
|
+
if (timeSinceLastHeartbeat > 60000) {
|
|
378
|
+
// No heartbeat for 60 seconds, consider dead
|
|
379
|
+
try {
|
|
380
|
+
connInfo.ws.close(1000, 'Heartbeat timeout');
|
|
381
|
+
} catch {
|
|
382
|
+
// Already closed
|
|
383
|
+
}
|
|
384
|
+
clearInterval(interval);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
this.send(connectionId, 'ping', { timestamp: Date.now() });
|
|
389
|
+
}, 30000); // Every 30 seconds
|
|
390
|
+
|
|
391
|
+
return interval;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Get room information
|
|
396
|
+
*/
|
|
397
|
+
getRoomInfo(roomId) {
|
|
398
|
+
const room = this.rooms.get(roomId);
|
|
399
|
+
if (!room) {return null;}
|
|
400
|
+
|
|
401
|
+
const members = Array.from(room).map(connId => {
|
|
402
|
+
const connInfo = this.connections.get(connId);
|
|
403
|
+
return {
|
|
404
|
+
connectionId: connId,
|
|
405
|
+
userId: connInfo?.userId,
|
|
406
|
+
connectedAt: connInfo?.connectedAt
|
|
407
|
+
};
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
return {
|
|
411
|
+
roomId,
|
|
412
|
+
memberCount: room.size,
|
|
413
|
+
members,
|
|
414
|
+
createdAt: Math.min(...members.map(m => m.connectedAt))
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Get user presence information
|
|
420
|
+
*/
|
|
421
|
+
getUserPresence(userId) {
|
|
422
|
+
const userConns = this.userConnections.get(userId);
|
|
423
|
+
if (!userConns) {return null;}
|
|
424
|
+
|
|
425
|
+
const connections = Array.from(userConns).map(connId => {
|
|
426
|
+
const connInfo = this.connections.get(connId);
|
|
427
|
+
return {
|
|
428
|
+
connectionId: connId,
|
|
429
|
+
connectedAt: connInfo?.connectedAt,
|
|
430
|
+
rooms: Array.from(connInfo?.rooms || [])
|
|
431
|
+
};
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
return {
|
|
435
|
+
userId,
|
|
436
|
+
connectionCount: userConns.size,
|
|
437
|
+
connections,
|
|
438
|
+
isOnline: userConns.size > 0
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Get all active rooms
|
|
444
|
+
*/
|
|
445
|
+
getAllRooms() {
|
|
446
|
+
const roomList = [];
|
|
447
|
+
|
|
448
|
+
for (const [roomId, members] of this.rooms.entries()) {
|
|
449
|
+
roomList.push({
|
|
450
|
+
roomId,
|
|
451
|
+
memberCount: members.size
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return roomList;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Get connection statistics
|
|
460
|
+
*/
|
|
461
|
+
getStats() {
|
|
462
|
+
return {
|
|
463
|
+
activeConnections: this.connections.size,
|
|
464
|
+
activeRooms: this.rooms.size,
|
|
465
|
+
activeUsers: this.userConnections.size,
|
|
466
|
+
queuedMessages: this.messageQueue.length,
|
|
467
|
+
messageHandlers: this.messageHandlers.size
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Close all connections gracefully
|
|
473
|
+
*/
|
|
474
|
+
shutdown() {
|
|
475
|
+
// Clear all heartbeat intervals
|
|
476
|
+
for (const interval of this.heartbeatIntervals.values()) {
|
|
477
|
+
clearInterval(interval);
|
|
478
|
+
}
|
|
479
|
+
this.heartbeatIntervals.clear();
|
|
480
|
+
|
|
481
|
+
for (const [, connInfo] of this.connections.entries()) {
|
|
482
|
+
try {
|
|
483
|
+
connInfo.ws.close(1001, 'Server shutting down');
|
|
484
|
+
} catch {
|
|
485
|
+
// Already closed
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
this.connections.clear();
|
|
489
|
+
this.rooms.clear();
|
|
490
|
+
this.userConnections.clear();
|
|
491
|
+
this.messageHandlers.clear();
|
|
492
|
+
this.messageQueue = [];
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* WebSocket Client Helper
|
|
498
|
+
* For use in Bun HTTP server context
|
|
499
|
+
*/
|
|
500
|
+
export class WebSocketClient {
|
|
501
|
+
constructor(url, userId = null) {
|
|
502
|
+
this.url = url;
|
|
503
|
+
this.userId = userId;
|
|
504
|
+
this.ws = null;
|
|
505
|
+
this.reconnectAttempts = 0;
|
|
506
|
+
this.maxReconnectAttempts = 10;
|
|
507
|
+
this.reconnectDelay = 1000; // Start at 1 second
|
|
508
|
+
this.messageHandlers = new Map();
|
|
509
|
+
this.roomSubscriptions = new Set();
|
|
510
|
+
this.isConnecting = false;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Connect to WebSocket server
|
|
515
|
+
*/
|
|
516
|
+
connect() {
|
|
517
|
+
if (this.isConnecting || this.ws) {return;}
|
|
518
|
+
this.isConnecting = true;
|
|
519
|
+
|
|
520
|
+
try {
|
|
521
|
+
this.ws = new WebSocket(this.url);
|
|
522
|
+
|
|
523
|
+
this.ws.onopen = () => {
|
|
524
|
+
frameworkLog('log', 'Connected to WebSocket server');
|
|
525
|
+
this.reconnectAttempts = 0;
|
|
526
|
+
this.reconnectDelay = 1000;
|
|
527
|
+
this.emit('connected', { userId: this.userId });
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
this.ws.onmessage = (event) => {
|
|
531
|
+
const data = JSON.parse(event.data);
|
|
532
|
+
this.emit(data.type, data.payload);
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
this.ws.onerror = (error) => {
|
|
536
|
+
frameworkLog('error', 'WebSocket error:', error);
|
|
537
|
+
this.emit('error', error);
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
this.ws.onclose = () => {
|
|
541
|
+
frameworkLog('log', 'Disconnected from WebSocket');
|
|
542
|
+
this.ws = null;
|
|
543
|
+
this.isConnecting = false;
|
|
544
|
+
this.attemptReconnect();
|
|
545
|
+
};
|
|
546
|
+
} catch (error) {
|
|
547
|
+
frameworkLog('error', 'Failed to connect to WebSocket:', error);
|
|
548
|
+
this.isConnecting = false;
|
|
549
|
+
this.attemptReconnect();
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Attempt to reconnect with exponential backoff
|
|
555
|
+
*/
|
|
556
|
+
attemptReconnect() {
|
|
557
|
+
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
558
|
+
this.emit('reconnect:failed', {
|
|
559
|
+
attempts: this.reconnectAttempts
|
|
560
|
+
});
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
this.reconnectAttempts++;
|
|
565
|
+
const delay = Math.min(this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1), 30000);
|
|
566
|
+
|
|
567
|
+
frameworkLog('log', `Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`);
|
|
568
|
+
|
|
569
|
+
setTimeout(() => {
|
|
570
|
+
this.connect();
|
|
571
|
+
}, delay);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Register message handler
|
|
576
|
+
*/
|
|
577
|
+
on(type, handler) {
|
|
578
|
+
if (!this.messageHandlers.has(type)) {
|
|
579
|
+
this.messageHandlers.set(type, []);
|
|
580
|
+
}
|
|
581
|
+
this.messageHandlers.get(type).push(handler);
|
|
582
|
+
return this;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Emit event to handlers
|
|
587
|
+
*/
|
|
588
|
+
emit(type, payload) {
|
|
589
|
+
const handlers = this.messageHandlers.get(type) || [];
|
|
590
|
+
for (const handler of handlers) {
|
|
591
|
+
try {
|
|
592
|
+
handler(payload);
|
|
593
|
+
} catch (error) {
|
|
594
|
+
frameworkLog('error', `Error in handler for ${type}:`, error);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Send message to server
|
|
601
|
+
*/
|
|
602
|
+
send(type, payload) {
|
|
603
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
604
|
+
frameworkLog('warn', 'WebSocket not connected');
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
try {
|
|
609
|
+
this.ws.send(JSON.stringify({
|
|
610
|
+
type,
|
|
611
|
+
payload,
|
|
612
|
+
timestamp: Date.now()
|
|
613
|
+
}));
|
|
614
|
+
return true;
|
|
615
|
+
} catch (error) {
|
|
616
|
+
frameworkLog('error', 'Error sending message:', error);
|
|
617
|
+
return false;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Join a room
|
|
623
|
+
*/
|
|
624
|
+
joinRoom(roomId) {
|
|
625
|
+
this.roomSubscriptions.add(roomId);
|
|
626
|
+
return this.send('join:room', {
|
|
627
|
+
roomId,
|
|
628
|
+
userId: this.userId
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Leave a room
|
|
634
|
+
*/
|
|
635
|
+
leaveRoom(roomId) {
|
|
636
|
+
this.roomSubscriptions.delete(roomId);
|
|
637
|
+
return this.send('leave:room', {
|
|
638
|
+
roomId
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* Disconnect from server
|
|
644
|
+
*/
|
|
645
|
+
disconnect() {
|
|
646
|
+
if (this.ws) {
|
|
647
|
+
this.ws.close(1000, 'Client closing');
|
|
648
|
+
this.ws = null;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
export default {
|
|
654
|
+
WebSocketServer,
|
|
655
|
+
WebSocketClient
|
|
656
|
+
};
|