-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
131 lines (104 loc) · 3.51 KB
/
server.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = createServer((req, res) => {
const parsedUrl = parse(req.url, true);
const { pathname, query } = parsedUrl;
if (pathname === '/') {
app.render(req, res, '/', query);
} else if (pathname === '/channel') {
app.render(req, res, '/channel', query);
} else if (pathname === '/history') {
app.render(req, res, '/history', query);
} else if (pathname === '/main') {
app.render(req, res, '/history', query);
} else {
handle(req, res, parsedUrl);
}
}).listen(8080, (err) => {
if (err) throw err;
console.log('> Server started on http://localhost:8080');
});
const io = require('socket.io')(server);
const { EVENTS } = require('./src/constants/socketEvent');
const users = {};
const socketToChannel = {};
const readyPlayers = {};
io.on('connection', (socket) => {
console.log('socket connected...');
socket.on(EVENTS.CREATE_CHANNEL, (channel) => {
socket.broadcast.emit(EVENTS.LISTEN_CREATE_CHANNEL, channel);
});
socket.on(EVENTS.ENTER_CHANNEL, (user) => {
const { channelId } = user;
if (users[channelId]) {
users[channelId].push(socket.id);
} else {
users[channelId] = [socket.id];
}
socketToChannel[socket.id] = channelId;
const usersInThisChannel = users[channelId].filter(
(id) => id !== socket.id,
);
socket.emit(EVENTS.ALL_USER, usersInThisChannel);
socket.join(channelId);
io.emit(EVENTS.LISTEN_ENTER_CHANNEL, user);
});
socket.on(EVENTS.EXIT_CHANNEL, ({ channelId, userId }) => {
socket.leave(channelId);
const newUserInThisChannel = users[channelId].filter(
(id) => id !== userId,
);
users.channelId = newUserInThisChannel;
io.emit(EVENTS.LISTEN_EXIT_CHANNEL, {
channelId,
userId,
});
});
socket.on(EVENTS.SENDING_SIGNAL, ({ userToSignal, callerId, signal }) => {
io.to(userToSignal).emit(EVENTS.USER_JOINED, {
signal: signal,
callerId: callerId,
});
});
socket.on(EVENTS.RETURNING_SIGNAL, ({ callerId, signal }) => {
io.to(callerId).emit(EVENTS.RECEIVING_RETURNED_SIGNAL, {
signal: signal,
id: socket.id,
});
});
socket.on(EVENTS.NEW_CHAT, ({ channelId, newChat }) => {
socket.join(channelId);
io.to(channelId).emit(EVENTS.LISTEN_NEW_CHAT, { channelId, newChat });
});
socket.on(EVENTS.END_CHANNEL, (id) => {
delete users[id];
socket.broadcast.emit(EVENTS.LISTEN_END_CHANNEL, id);
});
socket.on(
EVENTS.PLAYER_READY,
({ channelId, userId, characterId, episode }) => {
io.to(channelId).emit(EVENTS.LISTEN_PLAYER_READY, {
userId,
characterId,
});
if (!readyPlayers[channelId]) {
readyPlayers[channelId] = [characterId];
} else {
readyPlayers[channelId].push(characterId);
}
if (readyPlayers[channelId].length === episode.characters.length) {
io.to(channelId).emit(EVENTS.LISTEN_GAME_START, 'start');
delete readyPlayers[channelId];
}
},
);
socket.on(EVENTS.READY_TO_START, (channelId) => {
io.to(channelId).emit(EVENTS.LISTEN_READY_TO_START, channelId);
});
});
});