-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
115 lines (99 loc) · 2.88 KB
/
app.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
const koa = require('koa');
const serve = require('koa-static');
const uuid = require('node-uuid');
const app = koa();
const server = require('http').createServer(app.callback());
/** io */
const io = require('socket.io')(server, { path: '/code' });
io.on('connection', function () { /* … */ });
/** io */
app.use(serve(__dirname + '/dist'));
server.listen(process.env.PORT || 5000);
io.on('connection', (socket) => {
let isInitialized = () => { //fixme change to bool value?
return socket.roomUuid;
}
const leaveRoom = () => {
socket.roomUuid && socket.leave(socket.roomUuid);
socket.roomUuid = null;
}
socket.on('create room', () => {
leaveRoom();
//fixme username?
socket.roomUuid = uuid.v4();
socket.join(socket.roomUuid);
socket.emit('create room', socket.roomUuid);
});
socket.on('join room', (roomUuid) => {
//fixme username?
//fixme what if room was not created?
if (!roomUuid) {
return;
}
leaveRoom();
socket.roomUuid = roomUuid;
socket.join(socket.roomUuid);
socket.emit('joined room', socket.roomUuid);
})
socket.on('load file', (loadFileSocketData) => {
if (!isInitialized()) {
return;
}
socket.broadcast.to(socket.roomUuid).emit('load file', loadFileSocketData);
})
socket.on('code change', (change/*{ code: string, filePath: string[] }*/) => {
if (!isInitialized()) {
return;
}
socket.broadcast.to(socket.roomUuid).emit('code change', {
username: socket.username,
change: change
});
});
socket.on('request files structure', (code) => {
if (!isInitialized()) {
return;
}
socket.broadcast.to(socket.roomUuid).emit('request files structure');
});
socket.on('files structure', (files/*{children: TreeFile[]}*/) => {
if (!isInitialized()) {
return;
}
socket.broadcast.to(socket.roomUuid).emit('files structure', files);
});
socket.on('add user', (username) => {
if (addedUser) return;
socket.username = username;
socket.emit('login', {
//FIXME
});
// echo globally (all clients) that a person has connected
socket.broadcast.to(socket.roomUuid).emit('user joined', {
username: socket.username
});
});
/*
// when the client emits 'typing', we broadcast it to others
socket.on('typing', () => {
socket.broadcast.to(socket.roomUuid).emit('typing', {
username: socket.username
});
});
// when the client emits 'stop typing', we broadcast it to others
socket.on('stop typing', () => {
socket.broadcast.to(socket.roomUuid).emit('stop typing', {
username: socket.username
});
});
*/
// when the user disconnects perform this
socket.on('disconnect', () => {
if (false) { //TODO FIXME
// echo globally that this client has left
socket.broadcast.emit('user left', {
username: socket.username
});
}
});
});