-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.ts
54 lines (44 loc) · 1.54 KB
/
Server.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Server, Socket } from 'socket.io';
import { DB } from './Database';
import http from 'http';
import { MessageHandler } from './MessageHandler';
import { JoinHandler } from './handlers/JoinHandler';
import { GetMembersHandler } from './handlers/GetMembersHandler';
const VALID_ORIGINS = ['http://localhost:3000', 'http://localhost:3001'];
const PING_TIMEOUT_MS = 10000;
const PING_INTERVAL_MS = 10000;
const WS_PATH = '/ws';
// Add messages you'd like to support here
export const validMessages: MessageHandler[] = [JoinHandler, GetMembersHandler];
function disconnectHandler(socket: Socket): (reason: string) => void {
return reason => {
console.debug(`disconnect[${socket.id}]: ${reason}`);
};
}
function connectHandler(socket: Socket) {
socket.once('disconnect', disconnectHandler(socket));
socket.onAny((event, msg) =>
console.debug(`${event}[${socket.id}]: ${JSON.stringify(msg)}`),
);
validMessages.forEach(handler => handler.attach(socket));
}
export async function initServer(port: number): Promise<Server> {
const httpServer = http.createServer();
// FIXME: production release
// httpServer.listen(port, 'localhost');
httpServer.listen(3002, 'localhost');
const server = new Server({
path: WS_PATH,
serveClient: false,
pingTimeout: PING_TIMEOUT_MS,
pingInterval: PING_INTERVAL_MS,
cors: {
origin: VALID_ORIGINS,
},
transports: ['websocket'],
});
console.debug(`opening socket on port ${port}`);
server.on('connection', connectHandler);
server.attach(httpServer);
return server;
}