-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (54 loc) · 1.4 KB
/
server.js
File metadata and controls
65 lines (54 loc) · 1.4 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
// Depedencies
var express = require('express');
var http = require('http');
var path = require('path');
var socketIO = require('socket.io');
var app = express();
var server = http.Server(app);
var io = socketIO(server);
// Serve on port 5000
app.set('port', 5000);
app.use('/static', express.static(__dirname + '/static'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "vps.tonychouteau.fr"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Routing
app.get('/', function (request, response) {
response.sendFile(path.join(__dirname, 'index.html'));
});
// Starts the server.
server.listen(5000, function () {
console.log('Starting server on port 5000');
});
//Variables
let players = {};
// Add the WebSocket handlers
io.on('connection', function (socket) {
socket.on('new', function () {
console.log("New player : "+socket.id)
players[socket.id] = {
x:450.0,
y:450.0,
}
});
socket.on('move', function (data) {
var player = players[socket.id] || {};
if (data.left) {
player.x -= 1;
}
if (data.up) {
player.y -= 1;
}
if (data.right) {
player.x += 2;
}
if (data.down) {
player.y += 2;
}
});
});
setInterval(function() {
io.sockets.emit('state', players);
}, 1000/60);