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