2016-04-10 08:37:05 +00:00
|
|
|
import _ from 'lodash'
|
2016-04-14 04:01:51 +00:00
|
|
|
|
|
|
|
import { reset, list } from './content/routes'
|
2016-04-10 08:37:05 +00:00
|
|
|
|
|
|
|
export function register(ctx, name, method) {
|
|
|
|
if (_.isPlainObject(method)) {
|
|
|
|
Object.keys(method).forEach(key => {
|
|
|
|
register(ctx, [name, key].join('.'), method[key])
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.socket.on(name, async (data) => {
|
2016-04-14 04:01:51 +00:00
|
|
|
ctx.log.info('Got event', name)
|
2016-04-10 08:37:05 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await method(ctx, data)
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
ctx.log.error(error, `Error processing ${name}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function newConnection(ctx) {
|
|
|
|
ctx.log.info('Got new socket connection')
|
|
|
|
|
2016-04-14 04:01:51 +00:00
|
|
|
list(ctx)
|
|
|
|
reset(ctx)
|
2016-04-10 08:37:05 +00:00
|
|
|
}
|