forked from CGTAdal/PeerChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·44 lines (35 loc) · 1.36 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
var static = require('node-static');
var https = require('https');
var file = new(static.Server)();
var fs = require('fs');
var options = {
//key: fs.readFileSync('HTTPS_Permissions/key.pem'),
//cert: fs.readFileSync('HTTPS_Permissions/cert.pem')
}; // Here the Permissions related to HTTPS are stored in the HTTPS_Permissions Folder
var app = https.createServer( options, function(req, res) {
file.serve(req, res);
}).listen(process.env.PORT || 3000);
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {
socket.on('message', function(message) {
socket.broadcast.emit('message', message);
});
socket.on('chat', function(message) {
socket.broadcast.emit('chat', message);
});
socket.on('create or join', function(room) {
var numClients = io.sockets.clients(room).length;
if (numClients === 0) {
socket.join(room);
socket.emit('created', room);
} else if (numClients == 1) {
io.sockets. in (room).emit('join', room);
socket.join(room);
socket.emit('joined', room);
} else {
socket.emit('full', room);
}
socket.emit('emit(): client ' + socket.id + ' joined room ' + room);
socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room);
});
});