-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
70 lines (60 loc) · 2.98 KB
/
start.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
/* eslint-disable no-console */
const socketio = require('socket.io');
const app = require('./app');
const config = require('./config');
const server = app.listen(config.servicePort, config.serviceIP, () => {
// eslint-disable-next-line no-console
console.log(`Express is running on port ${server.address().port}`);
});
let sharedInMemoryObject = {}
const io = socketio(server, { cors: true, origins: '*:*' });
io.on('connection', (socket) => {
// socket.on('enter', (class_hash) => {
// socket.join(class_hash)
// if (sharedInMemoryObject[class_hash]) {
// console.log('someone entered an ongoing class, sending chat and board state', sharedInMemoryObject[class_hash]);
// }
// else{
// console.log('someone entered a new class, setting states as null');
// sharedInMemoryObject[class_hash] = {board_history:[],chat_history:[]}
// }
// socket.emit('board_init', {board_history:sharedInMemoryObject[class_hash].board_history, class_hash:class_hash});
// socket.emit('chat_init', {chat_history:sharedInMemoryObject[class_hash].chat_history, class_hash:class_hash});
// })
// socket.on('chat', (data) => {
// data_json = JSON.parse(data);
// console.log('new chat message from UI', data_json);
// sharedInMemoryObject[data_json.class_hash].chat_history.push(data_json.chat);
// socket.to(data_json.class_hash).emit('chat', {chat:data_json.chat, class_hash: data_json.class_hash});
// });
// socket.on('board', (data) => {
// data_json = JSON.parse(data);
// console.log('new board state from UI', data_json);
// sharedInMemoryObject[data_json.class_hash].board_history.push(data_json.fen);
// socket.to(data_json.class_hash).emit('board', {fen:data_json.fen, class_hash:data_json.class_hash});
// });
socket.on('enter', (class_hash) => {
socket.join(class_hash)
if (sharedInMemoryObject[class_hash]) {
console.log('someone entered an ongoing class, sending chat and board state', sharedInMemoryObject[class_hash]);
}
else {
console.log('someone entered a new class, setting states as null');
sharedInMemoryObject[class_hash] = { pgn: '', chat_history: [] }
}
socket.emit('board_init', { pgn: sharedInMemoryObject[class_hash].pgn, class_hash: class_hash });
socket.emit('chat_init', { chat_history: sharedInMemoryObject[class_hash].chat_history, class_hash: class_hash });
})
socket.on('chat', (data) => {
data_json = JSON.parse(data);
console.log('new chat message from UI', data_json);
sharedInMemoryObject[data_json.class_hash].chat_history.push(data_json.chat);
socket.to(data_json.class_hash).emit('chat', { chat: data_json.chat, class_hash: data_json.class_hash });
});
socket.on('board', (data) => {
data_json = JSON.parse(data);
console.log('new board state from UI', data_json);
sharedInMemoryObject[data_json.class_hash].pgn = data_json.pgn;
socket.to(data_json.class_hash).emit('board', { pgn: data_json.pgn, class_hash: data_json.class_hash });
});
});