@jcbuisson/express-x 2.0.3 → 2.1.0

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/server.mjs +15 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/server.mjs CHANGED
@@ -7,6 +7,11 @@ export function expressX(config) {
7
7
 
8
8
  const services = {}
9
9
  let appHooks = []
10
+ let socketConnectHandler = null
11
+
12
+ function onSocketConnect(func) {
13
+ socketConnectHandler = func
14
+ }
10
15
 
11
16
  const app = express()
12
17
  const httpServer = createServer(app)
@@ -33,29 +38,25 @@ export function expressX(config) {
33
38
  io.on('connection', async function(socket) {
34
39
  if (socket.recovered) {
35
40
  // recovery was successful: socket.id, socket.rooms and socket.data were restored
36
- console.log('reconnection!!!', socket.id)
37
-
41
+ // (network/Wifi disconnections)
42
+ console.log('reconnection!!!', socket.id, socket.data)
38
43
  } else {
39
- // new or unrecoverable session
40
- console.log("connection", socket.id)
44
+ // new or unrecoverable connection
45
+ // (page open, page refresh/reload)
46
+ socket.data.clientIP = socket.handshake.address
41
47
  }
42
48
 
43
- const clientIP = socket.handshake.address
44
- socket.data = {
45
- clientIP,
46
- }
47
- // app.log('verbose', `Client connected ${connectionId} from IP ${clientIP}`)
48
- app.log('verbose', `Client connected ${socket.id} from IP ${clientIP}`)
49
+ app.log('verbose', `Client connected ${socket.id}`)
49
50
 
50
51
  // emit 'connection' event for app (expressjs extends EventEmitter)
51
52
  app.emit('connection', socket)
52
53
 
54
+ if (socketConnectHandler) socketConnectHandler(socket)
55
+
53
56
  // send 'connected' event to client
54
- // socket.emit('connected', connectionId)
55
57
  socket.emit('connected', socket.id)
56
58
 
57
59
  socket.on('disconnect', () => {
58
- // app.log('verbose', `Client disconnected ${connectionId}`)
59
60
  app.log('verbose', `Client disconnected ${socket.id}`)
60
61
  })
61
62
 
@@ -180,7 +181,7 @@ export function expressX(config) {
180
181
  if (service.publishFunction) {
181
182
  // collect channel names to socket is member of
182
183
  const channelNames = await service.publishFunction(context)
183
- app.log('verbose', `publish channels ${service.name} ${methodName} ${channelNames}`)
184
+ app.log('verbose', `publish channels ${name} ${methodName} ${channelNames}`)
184
185
  // send event on all these channels
185
186
  if (channelNames.length > 0) {
186
187
  let sender = io.to(channelNames[0])
@@ -270,6 +271,7 @@ export function expressX(config) {
270
271
  joinChannel,
271
272
  leaveChannel,
272
273
  sendAppEvent,
274
+ onSocketConnect,
273
275
  })
274
276
 
275
277
  }