-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
66 lines (52 loc) · 1.52 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var https = require('https');
var fs = require('fs'); // Using the filesystem module
var url = require('url');
var options = {
key: fs.readFileSync('my-key.pem'),
cert: fs.readFileSync('my-cert.pem')
};
function handleIt(req, res) {
var parsedUrl = url.parse(req.url);
var path = parsedUrl.pathname;
if (path == "/") {
path = "index.html";
}
fs.readFile(__dirname + path,
// Callback function for reading
function (err, fileContents) {
// if there is an error
if (err) {
res.writeHead(500);
return res.end('Error loading ' + req.url);
}
// Otherwise, send the data, the contents of the file
res.writeHead(200);
res.end(fileContents);
}
);
// Send a log message to the console
console.log("Got a request " + req.url);
}
var httpServer = https.createServer(options, handleIt);
httpServer.listen(8080);
let numUsers = 0;
let users = [];
let userPositions = [];
// WebSocket Portion
// WebSockets work with the HTTP server
var io = require('socket.io').listen(httpServer);
// Register a callback function to run when we have an individual connection
// This is run for each individual user that connects
io.sockets.on('connection',
// We are given a websocket object in our function
function(socket) {
console.log("We have a new client: " + socket.id);
socket.on('newuser', function(data) {
numUsers++;
users.push(socket.id);
console.log('user list: ');
});
socket.on('disconnect', function() {
console.log("Client has disconnected");
});
});