-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsocket.ts
More file actions
56 lines (48 loc) · 1.13 KB
/
socket.ts
File metadata and controls
56 lines (48 loc) · 1.13 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { instrument } from "@socket.io/admin-ui";
import type { Server as HttpdServer } from "http";
import { Server } from "socket.io";
import type { SessionUser } from "~dm-types/SessionUser";
class ServerWithUser extends Server<
Record<string, never>,
Record<string, never>,
Record<string, never>,
{ user?: SessionUser }
> {}
export default (port: number, httpServer?: HttpdServer) => {
let io: ServerWithUser;
if (httpServer) {
httpServer.listen(port);
io = new ServerWithUser(httpServer, {
cors: {
origin: true,
},
});
} else {
io = new ServerWithUser({
cors: {
origin: true,
},
});
}
instrument(io, {
auth: false,
});
io.use((_socket, next) => {
process.on("unhandledRejection", (reason: Error) => {
console.error(reason);
next(reason);
});
process.on("uncaughtException", (error: Error) => {
console.error(error);
next(error);
});
next();
});
if (!httpServer) {
io.listen(port);
}
console.log(
`WebSocket open on port ${port} on worker ${process.env.NODE_APP_INSTANCE}`,
);
return io;
};