forked from chralp/heyburrito
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
25 lines (21 loc) · 812 Bytes
/
index.ts
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
import * as log from 'bog';
import ws from 'ws';
import BurritoStore from '../store/BurritoStore';
import config from '../config';
export default () => {
const wss = new ws.Server({ port: config.http.wss_port });
log.info(`WebSocketServer started on port ${config.http.wss_port}`);
wss.broadcast = (data: any) => {
wss.clients.forEach((client: any) => {
if (client.readyState === ws.OPEN) {
client.send(data);
}
});
};
BurritoStore.on('GIVE', async (to: string, from: string) => {
wss.broadcast(JSON.stringify({ event: 'GIVE', data: { to, from } }));
});
BurritoStore.on('TAKE_AWAY', async (to: string, from: string) => {
wss.broadcast(JSON.stringify({ event: 'TAKE_AWAY', data: { to, from } }));
});
};