2020-09-01 17:31:38 +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) {
|
2020-09-07 00:47:53 +00:00
|
|
|
ctx.log.debug('SocketIO: ' + name)
|
2020-09-01 17:31:38 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await method(ctx, data, cb)
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
ctx.log.error(error, `Error processing ${name}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-07 00:47:53 +00:00
|
|
|
function onConnection(server, config, db, log, coreService, data) {
|
2020-09-01 17:31:38 +00:00
|
|
|
const io = server
|
|
|
|
const socket = data
|
|
|
|
|
|
|
|
const child = log.child({
|
|
|
|
id: socket.id,
|
|
|
|
})
|
|
|
|
|
|
|
|
child.info('Got new socket connection')
|
|
|
|
|
|
|
|
let ctx = {
|
|
|
|
config,
|
|
|
|
io,
|
|
|
|
socket,
|
|
|
|
log: child,
|
|
|
|
db,
|
2020-09-07 00:47:53 +00:00
|
|
|
core: coreService,
|
2020-09-01 17:31:38 +00:00
|
|
|
logroot: log,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.socket.on('disconnect', function() {
|
|
|
|
child.info('Closed connection')
|
|
|
|
})
|
|
|
|
|
|
|
|
register(ctx, 'core', core)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default onConnection
|