forked from X-Profiler/xtransit-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatch.js
35 lines (31 loc) · 1.04 KB
/
dispatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';
const http = require('http');
const WebSocket = require('ws');
const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const config = require('./config');
const routes = require('./routes');
const logger = require('./proxy/logger');
const checkSign = require('./middleware/checkSign');
const messageHandler = require('./handler/message');
const closeHandler = require('./handler/close');
// http server
const app = new Koa();
app
.use(bodyParser())
.use(checkSign);
const router = new Router();
routes(router);
app
.use(router.routes())
.use(router.allowedMethods());
const server = http.createServer(app.callback());
// ws server
const wss = new WebSocket.Server({ server });
wss.on('connection', ws => {
ws.on('message', messageHandler.bind({ ws }));
ws.on('close', closeHandler.bind({ ws }));
});
server.on('error', err => logger.error(`server error: ${err}`));
server.listen(config.serverPort, () => console.log(`websockt server listening at ${config.serverPort}...`));