-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsocket.ts
More file actions
64 lines (57 loc) Β· 2.19 KB
/
socket.ts
File metadata and controls
64 lines (57 loc) Β· 2.19 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
57
58
59
60
61
62
63
64
import app from './app';
import { Socket } from './node_modules/socket.io/dist/socket';
import workspaceChecker from './middlewares/workspaceChecker';
import socketChat from './controllers/workspace/socketChat';
import socketKanban from './controllers/workspace/socketKanban';
import socketWebRTC from './controllers/workspace/socketWebRTC';
import * as dotenv from 'dotenv';
dotenv.config();
const http = require('http');
const server = http.createServer(app);
const io_s = require('socket.io');
const io = io_s(server);
const { createClient } = require('redis');
const redisAdapter = require('@socket.io/redis-adapter');
try {
const pubClient = createClient({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASSWORD,
});
const subClient = pubClient.duplicate();
console.log('π redis pub/sub setting done');
io.adapter(redisAdapter(pubClient, subClient));
// TODO: chat κΈ°λ₯ socket ν΅μ
const chatIo = io.of(/^\/chat\/\w{4,20}$/); // dynamic namespace(/chat/projectURL)
chatIo.use(workspaceChecker);
chatIo.on('connection', (socket: Socket) => {
console.log('π/chat#connection\n', socket.handshake.query);
const projectChatIo = chatIo.nsp;
app.set('chatIo', projectChatIo);
socketChat(socket);
});
// TODO: kanban κΈ°λ₯ socket ν΅μ
const kanbanIo = io.of(/^\/kanban\/\w{4,20}$/); // dynamic namespace(/kanban/projectURL)
kanbanIo.use(workspaceChecker);
kanbanIo.on('connection', (socket: Socket) => {
console.log('π/kanban#connection\n', socket.handshake.query);
const projectKanbanIo = kanbanIo.nsp;
app.set('kanbanIo', projectKanbanIo);
socketKanban(socket);
socket.emit('connection');
});
// TODO: WebRTC κΈ°λ₯ socket ν΅μ
const webRTCIo = io.of(/^\/webRTC\/\w{4,20}$/); // dynamic namespace(/webRTC/projectURL)
webRTCIo.use(workspaceChecker);
webRTCIo.on('connection', (socket: Socket) => {
console.log('π/webRTC#connection\n', socket.handshake.query);
const projectWebRTCIo = webRTCIo.nsp;
app.set('webRTCIo', projectWebRTCIo);
socketWebRTC(socket);
console.log(socket.handshake.query);
socket.emit('connection');
});
} catch (err) {
console.log('π redis pub/sub-err:', err.message);
}
module.exports = server;