-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
76 lines (57 loc) · 1.93 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
console.log("Starting server...");
// Import modules
var express = require('express');
var path = require('path');
// Initialize the application and socket IO connections
var app = express();
// Import game file here.
app.use(express.static('framework'));
var server = require('http').createServer(app).listen(process.env.PORT || 8001);
// Create the Socket.IO server and attach it to the HTTP server
var io = require('socket.io').listen(server);
var playerSocket = null;
io.of("/player").on('connection', function(socket) {
console.log('The player connected');
playerSocket = socket;
//Whenever someone disconnects this piece of code executed
socket.on('disconnect', function () {
console.log('The player disconnected');
});
});
io.of("/controller").on('connection', function(socket) {
console.log('A controller connected');
socket.on('load game', function(msg) {
console.log("loading game: " + msg);
if (playerSocket != null) {
playerSocket.emit('load game', msg);
}
});
socket.on('touch start', function(msg) {
if (playerSocket != null) {
playerSocket.emit('touch start', msg + ":" + socket.id);
}
});
socket.on('touch end', function(msg) {
if (playerSocket != null) {
playerSocket.emit('touch end', msg + ":" + socket.id);
}
});
socket.on('touch move', function(msg) {
if (playerSocket != null) {
playerSocket.emit('touch move', msg + ":" + socket.id);
}
});
socket.on('clicked', function(msg) {
if(playerSocket != null) {
playerSocket.emit('on click', msg + ":" + socket.id);
}
});
socket.on('disconnect', function () {
console.log('A controller disconnected.');
})
});
// Catch all error messages
process.on('uncaughtException', function (err) {
console.error(err.stack);
console.log("Node NOT Exiting...");
});