2020-09-08 07:53:42 +00:00
|
|
|
import * as core from './core/ioroutes.mjs'
|
|
|
|
|
|
|
|
function register(ctx, name, method) {
|
|
|
|
if (typeof(method) === 'object') {
|
|
|
|
Object.keys(method).forEach(key => {
|
|
|
|
register(ctx, [name, key].join('.'), method[key])
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.socket.on(name, async function(data, cb) {
|
|
|
|
ctx.log.debug('SocketIO: ' + name)
|
|
|
|
|
|
|
|
try {
|
|
|
|
await method(ctx, data, cb)
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
ctx.log.error(error, `Error processing ${name}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-28 07:27:18 +00:00
|
|
|
function onConnection(server, ctx, data) {
|
2020-09-08 07:53:42 +00:00
|
|
|
const io = server
|
|
|
|
const socket = data
|
|
|
|
|
2022-03-28 07:27:18 +00:00
|
|
|
const child = ctx.log.child({
|
2020-09-08 07:53:42 +00:00
|
|
|
id: socket.id,
|
|
|
|
})
|
2022-03-28 07:27:18 +00:00
|
|
|
child.event = ctx.log.event
|
2020-09-08 07:53:42 +00:00
|
|
|
|
|
|
|
child.info('Got new socket connection')
|
|
|
|
|
2022-03-28 07:27:18 +00:00
|
|
|
let ioCtx = {
|
|
|
|
io: io,
|
|
|
|
socket: socket,
|
2020-09-08 07:53:42 +00:00
|
|
|
log: child,
|
2022-03-28 07:27:18 +00:00
|
|
|
db: ctx.db,
|
|
|
|
core: ctx.core,
|
|
|
|
logroot: ctx.log,
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 07:27:18 +00:00
|
|
|
ioCtx.socket.on('disconnect', function() {
|
2020-09-08 07:53:42 +00:00
|
|
|
child.info('Closed connection')
|
|
|
|
})
|
|
|
|
|
2022-03-28 07:27:18 +00:00
|
|
|
register(ioCtx, 'core', core)
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default onConnection
|