forked from sbidolach/mobile-gamepad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·55 lines (46 loc) · 1.35 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
var express = require('express');
var websocket = require('socket.io');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var gamehub = require('./server/src/gamehub');
app.use(express.static('./dist/public'));
app.get('/', function(req, res){
res.render('index');
})
io.on('connection', function(socket) {
console.log('gamepad connected');
socket.on('disconnect', function() {
if(socket.inputId !== undefined){
console.log('goodbye input -> ' + socket.inputId);
gamehub.disconnect(socket.inputId);
}
console.log('gamepad disconnected');
return null;
});
socket.on('hello', function() {
gamehub.connect(function(inputId){
if (inputId !== -1) {
socket.inputId = inputId;
console.log('hello input -> ' + socket.inputId);
socket.emit('hello', {
inputId: inputId
})
}
return null;
});
return null;
});
socket.on('event', function(code) {
// console.log('event -> ' + code + ' input -> ' + socket.inputId);
if(socket.inputId !== undefined && code){
gamehub.sendEvent(socket.inputId, code);
}
return null;
});
});
// Start the server on port provided by grunt-express-server
http.listen(8888, function() {
console.log('Express server listening on port ' + 8888);
});
module.exports = app;