-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (55 loc) · 1.77 KB
/
Copy pathserver.js
File metadata and controls
71 lines (55 loc) · 1.77 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
var static = require('node-static');
var file = new(static.Server)();
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync('private.key', 'utf8');
var certificate = fs.readFileSync('server.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
// your express configuration here
var httpServer = http.createServer(function (req, res) {
file.serve(req, res);
});
var httpsServer = https.createServer(credentials, function (req, res) {
file.serve(req, res);
});
httpServer.listen(1234,"192.168.150.170");
httpsServer.listen(12345,"192.168.150.170");
app.get('/', function (req, res) {
res.render('index', {});
});
var io = require('socket.io').listen(httpsServer);
io.sockets.on('connection', function (socket) {
function log() {
var array = [">>> "];
for(var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}
socket.on('message', function (message) {
log('Got message: ', message);
socket.broadcast.emit('message', message); // should be room only
});
socket.on('create or join', function (room) {
var numClients = io.sockets.clients(room).length;
log('Room ' + room + ' has ' + numClients + ' client(s)');
log('Request to create or join room', room);
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 { // max two clients
socket.emit('full', room);
}
socket.emit('emit(): client ' + socket.id + ' joined room ' + room);
socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room);
});
});