-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
39 lines (34 loc) · 1.2 KB
/
index.ts
File metadata and controls
39 lines (34 loc) · 1.2 KB
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
36
37
38
39
import serveStatic from 'serve-static';
import bodyParser from 'body-parser';
import cors from 'cors';
import { Server } from 'ws';
import router from './server/helpers/router';
import './server/api';
import './server/auth';
export default (app, server) => {
app.use(bodyParser.json({ limit: '20mb' }));
app.use(bodyParser.urlencoded({ limit: '20mb', extended: false }));
app.use(cors());
app.use(serveStatic(`${__dirname}/dist`));
app.get('*', (req, res) => res.sendFile(`${__dirname}/dist/index.html`));
const wss = new Server({ server });
router.wss = wss;
wss.on('connection', ws => {
console.log('Got connection from new peer');
ws.on('error', () => console.log('Error on connection with peer'));
ws.on('close', () => console.log('Connection with peer closed'));
ws.on('message', async message => {
let call = {};
try {
call = JSON.parse(message);
} catch (e) {
console.error(e);
}
if (call[0] && call[0] === 'request' && call[1] && call[1].command) {
const { command, params, tag } = call[1];
await router.render(command, params, tag, ws);
}
});
});
}
//both express server, websocket and client running together