this is podracing
This commit is contained in:
1 parent
65d37eeb61
commit
f2294def05
12 files changed
+187
-169
No files matched your search
+27
-33
@@ -22,10 +22,7 @@ export default function createApi(log: Log, pg: Pg, discogs, offers, events) {
|
||||
const WS_PORT = 40055;
|
||||
const SECRET = 'ohja4Eif9eidah3aj5ta2haa4iemooth';
|
||||
|
||||
const app = new koa();
|
||||
const server = http.createServer(app.callback());
|
||||
const wsApp = koaWebsocket(new koa());
|
||||
const wsServer = http.createServer(wsApp.callback());
|
||||
const app = koaWebsocket(new koa());
|
||||
|
||||
app.use(async (ctx, next) => {
|
||||
try {
|
||||
@@ -42,18 +39,8 @@ export default function createApi(log: Log, pg: Pg, discogs, offers, events) {
|
||||
const router = koaRouter();
|
||||
|
||||
function listen() {
|
||||
return new Promise((resolve, reject) => {
|
||||
app.on('error', err => {
|
||||
reject(err);
|
||||
})
|
||||
|
||||
server.listen(PORT, () => {
|
||||
log.info('listening', { PORT });
|
||||
});
|
||||
|
||||
wsApp.listen(WS_PORT);
|
||||
log.info('ws listening', { WS_PORT });
|
||||
});
|
||||
const server = app.listen(PORT);
|
||||
log.info({ PORT }, 'listening');
|
||||
}
|
||||
|
||||
app.use(cors({ credentials: true }));
|
||||
@@ -160,27 +147,34 @@ export default function createApi(log: Log, pg: Pg, discogs, offers, events) {
|
||||
ctx.body = { success: true };
|
||||
});
|
||||
|
||||
const wsRouter = koaRouter();
|
||||
// Using routes
|
||||
wsRouter.get('/ws', async (ctx, next) => {
|
||||
// `ctx` is the regular koa context created from the `ws` onConnection `socket.upgradeReq` object.
|
||||
// the websocket is added to the context on `ctx.websocket`.
|
||||
ctx.websocket.send('Hello World');
|
||||
ctx.websocket.on('message', (message) => {
|
||||
// do something with the message from client
|
||||
if (message.toString() === 'ping') {
|
||||
ctx.websocket.send('pong');
|
||||
}
|
||||
});
|
||||
return next;
|
||||
});
|
||||
|
||||
app.use(router.routes());
|
||||
wsApp.ws.use(wsRouter.routes());
|
||||
|
||||
app.ws.use(passport.initialize());
|
||||
app.ws.use(passport.session());
|
||||
app.ws.use(async (ctx, next) => {
|
||||
log.info('new ws connection', { user: ctx.state.user });
|
||||
ctx.websocket.on('message', async function incoming(message) {
|
||||
log.info(message.toString());
|
||||
try {
|
||||
const parsed = JSON.parse(message.toString());
|
||||
log.info(parsed);
|
||||
// const response = await rpc.receive(parsed);
|
||||
// rpc.send(ws, response);
|
||||
} catch (e) {
|
||||
log.warn(e);
|
||||
log.warn(message.toString());
|
||||
// rpc.send(ws, [RpcMessageType.Error, e.message || e.msg])
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
ctx.websocket.on('close', function onClose() {
|
||||
log.info('ws disconnected', { user: ctx.state.user });
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
app,
|
||||
server,
|
||||
listen,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user