@jcbuisson/express-x 1.4.2 → 1.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/server.mjs +19 -8
package/package.json
CHANGED
package/src/server.mjs
CHANGED
|
@@ -17,6 +17,8 @@ export function expressX(prisma, options = {}) {
|
|
|
17
17
|
|
|
18
18
|
app.connections = {}
|
|
19
19
|
let lastConnectionId = options.initialConnectionId || 1
|
|
20
|
+
// TODO: use redis to store `connections` and `lastConnectionId` for a clustered deployment
|
|
21
|
+
// (a connection/reconnection may occur on two different servers)
|
|
20
22
|
|
|
21
23
|
app.printCnx = (label) => {
|
|
22
24
|
console.log(label)
|
|
@@ -256,18 +258,17 @@ export function expressX(prisma, options = {}) {
|
|
|
256
258
|
const io = new Server(server)
|
|
257
259
|
|
|
258
260
|
io.on('connection', function(socket) {
|
|
259
|
-
app.log('verbose', 'Client connected to the WebSocket')
|
|
260
261
|
const connection = {
|
|
261
262
|
id: lastConnectionId++,
|
|
263
|
+
createdAt: new Date(),
|
|
262
264
|
socket,
|
|
263
265
|
channelNames: new Set(),
|
|
264
|
-
data: {
|
|
265
|
-
a: 123
|
|
266
|
-
},
|
|
266
|
+
data: {},
|
|
267
267
|
}
|
|
268
|
+
app.log('verbose', `Client connected ${connection.id}`)
|
|
268
269
|
// store connection in cache
|
|
269
270
|
app.connections[connection.id] = connection
|
|
270
|
-
app.log('verbose', `Connection ids: ${Object.keys(app.connections)}`)
|
|
271
|
+
// app.log('verbose', `Connection ids: ${Object.keys(app.connections)}`)
|
|
271
272
|
|
|
272
273
|
// emit 'connection' event for app (expressjs extends EventEmitter)
|
|
273
274
|
app.emit('connection', connection)
|
|
@@ -285,6 +286,11 @@ export function expressX(prisma, options = {}) {
|
|
|
285
286
|
}, 60 * 1000)
|
|
286
287
|
})
|
|
287
288
|
|
|
289
|
+
socket.on('reconnect', () => {
|
|
290
|
+
console.log('Client reconnected:', socket.id)
|
|
291
|
+
// Add your custom logic to link the reconnected WebSocket with the previous session
|
|
292
|
+
})
|
|
293
|
+
|
|
288
294
|
|
|
289
295
|
// handle connection data transfer caused by a disconnection/reconnection (page reload, network issue, etc.)
|
|
290
296
|
socket.on('cnx-transfer', async ({ from, to }) => {
|
|
@@ -293,16 +299,21 @@ export function expressX(prisma, options = {}) {
|
|
|
293
299
|
app.connections[to] = app.connections[from]
|
|
294
300
|
app.connections[to].socket = socket
|
|
295
301
|
delete app.connections[from]
|
|
296
|
-
|
|
302
|
+
app.printCnx('AFTER TRANSFER')
|
|
303
|
+
// send acknowledge to client
|
|
304
|
+
io.emit('cnx-transfer-ack', to)
|
|
297
305
|
})
|
|
298
306
|
|
|
307
|
+
function wait(ms) {
|
|
308
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
309
|
+
}
|
|
299
310
|
|
|
300
311
|
/*
|
|
301
312
|
* Handle websocket client request
|
|
302
313
|
* Emit in return a 'client-response' message
|
|
303
314
|
*/
|
|
304
315
|
socket.on('client-request', async ({ uid, name, action, args }) => {
|
|
305
|
-
app.log('verbose', `client-request ${uid} ${name} ${action} ${args}`)
|
|
316
|
+
app.log('verbose', `client-request ${uid} ${name} ${action} ${JSON.stringify(args)}`)
|
|
306
317
|
if (name in services) {
|
|
307
318
|
const service = services[name]
|
|
308
319
|
try {
|
|
@@ -311,7 +322,7 @@ export function expressX(prisma, options = {}) {
|
|
|
311
322
|
const context = {
|
|
312
323
|
app,
|
|
313
324
|
transport: 'ws',
|
|
314
|
-
params: { connection, name, action, args },
|
|
325
|
+
params: { connectionId: connection.id, name, action, args },
|
|
315
326
|
}
|
|
316
327
|
|
|
317
328
|
try {
|