sc-manager/api/routerio.mjs

56 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2020-09-08 07:53:42 +00:00
import * as core from './core/ioroutes.mjs'
import { getConfig, getStatus } from './util.mjs'
2020-09-08 07:53:42 +00:00
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}`)
}
})
}
function onConnection(server, ctx, data) {
2020-09-08 07:53:42 +00:00
const io = server
const socket = data
const child = ctx.log.child({
2020-09-08 07:53:42 +00:00
id: socket.id,
})
child.event = ctx.log.event
2020-09-08 07:53:42 +00:00
child.info('Got new socket connection')
let ioCtx = {
io: io,
socket: socket,
2020-09-08 07:53:42 +00:00
log: child,
db: ctx.db,
core: ctx.core,
logroot: ctx.log,
2020-09-08 07:53:42 +00:00
}
ioCtx.socket.on('disconnect', function() {
2020-09-08 07:53:42 +00:00
child.info('Closed connection')
})
register(ioCtx, 'core', core)
ioCtx.socket.emit('core.config', getConfig(ioCtx))
ioCtx.socket.emit('core.status', getStatus(ioCtx))
2020-09-08 07:53:42 +00:00
}
export default onConnection