Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Apr 3, 2022. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
Latest commit
166 lines (139 loc) · 5.38 KB
/
Copy pathserver.js
File metadata and controls
166 lines (139 loc) · 5.38 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Imports
constexpress=require('express');
constapp=express();
constserver=require('http').Server(app);
constcompression=require('compression');
constminify=require('express-minify');
varss=require('socket.io-stream');
// File imports
constconfig=require('./config.js');
// Express middlewares
app.use(compression());
app.use(minify());
app.use(express.static('public'));
// API Routes
app.get('*',function(req,res){// The 404 Route (ALWAYS Keep this as the last route)
res.redirect(config.webserver.splash);
});
constio=require('socket.io')(server,{maxHttpBufferSize: config.api.maxSize});
// SocketIO
constrooms={}
io.on('connection',(socket)=>{
socket.on('chat event',(data)=>{
// Listens for chat messages from clients
if(typeofdata==='object'){
// Make sure the data received is a JS object
varnow=newDate();
if(data.roomName===undefined||data.user_name===undefined||data.message===undefined||data.hmac===undefined){
// Check if the fields are present
console.log(`${now} - Event had invalid fields.`)
return;
}
io.to(data.roomName).emit('chat response',{
// Broadcasts the message to the Socket room
user_name: data.user_name,
message: data.message,
hmac: data.hmac
});
return;
}
console.log(`${now} - Event was rejected: ${data}`)
});
socket.on('join',(data)=>{
// Listens for join events from clients
if(typeofdata==='object'){
// Make sure the data received is a JS object
varnow=newDate();
if(data.roomName===undefined||data.user_name===undefined){
// Make sure the data received is a JS object
console.log(`${now} - Event had invalid fields.`)
return;
}
// Join the socket to the requested room
socket.join(data.roomName);
io.to(data.roomName).emit('join response',{
// Broadcasts the username as a join response to the Socket room
user_name: data.user_name
});
// Update the in-mem user count
constuserCount=rooms[data.roomName];
if(userCount===undefined){
// If the user count doesn't exist
// set it to 1
rooms[data.roomName]=1;
io.to(data.roomName).emit('user count',{
count: 1
});
}else{
// Otherwise, add 1 to it
rooms[data.roomName]=userCount+1;
io.to(data.roomName).emit('user count',{
count: userCount+1
});
}
return;
}
console.log(`${now} - Event was rejected: ${data}`)
});
socket.on('leave',(data)=>{
// Listens for leave events from clients
socket.disconnect();
if(typeofdata==='object'){
// Make sure the data received is a JS object
varnow=newDate();
if(data.roomName===null||data.user_name===undefined){
// Make sure the data received is a JS object
console.log(`${now} - Event had invalid fields.`)
return;
}
// Leave the socket from the requested room
socket.leave(data.roomName)
io.to(data.roomName).emit('leave response',{
// Broadcasts the username as a leave response to the Socket room
user_name: data.user_name
});
// Update the in-mem user count
constuserCount=rooms[data.roomName];
if(userCount===undefined){
// If the user count doesn't exist
// do nothing
return;
}elseif(userCount===1){
// If the user is the only one in the room, set the count to 0
rooms[data.roomName]=0;
return;
}else{
// Otherwise, remove 1 from it
rooms[data.roomName]=userCount-1;
io.to(data.roomName).emit('user count',{
count: userCount-1
});
}
return;
}
console.log(`${now} - Event was rejected: ${data}`)
});
ss(socket).on('file event',function(stream,data){
// Listens for file uploads
varnow=newDate();
console.log(`${now} - File event received`)
if(data.user_name===undefined||data.name===undefined||data.type===undefined||data.data===undefined||data.hmac===undefined){
console.log(`${now} - Event had invalid fields.`);
return;
}
io.to(data.roomName).emit('file response',{
user_name: data.user_name,
name: data.name,
type: data.type,
data: data.data,
hmac: data.hmac
});
io.to(socket.id).emit('file progress',{
finished: true
});
});
});
// Server start
server.listen(config.webserver.port,config.webserver.host,()=>{
console.log('listening on *:'+config.webserver.port);
});