-
Notifications
You must be signed in to change notification settings - Fork 52
/
server.js
146 lines (123 loc) · 4.19 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Generated by CoffeeScript 1.10.0
/*
Created by MIROOF on 04/03/2015
Virtual gamepad application
*/
(function() {
var app, config, express, gamepad_hub, gp_hub, http, io, kb_hub, keyboard_hub, log, path, port, suffix, touchpad_hub, tp_hub;
path = require('path');
express = require('express');
app = express();
http = require('http').Server(app);
io = require('socket.io')(http);
config = require('./config.json');
log = require('./lib/log');
gamepad_hub = require('./app/virtual_gamepad_hub');
gp_hub = new gamepad_hub();
keyboard_hub = require('./app/virtual_keyboard_hub');
kb_hub = new keyboard_hub();
touchpad_hub = require('./app/virtual_touchpad_hub');
tp_hub = new touchpad_hub();
port = process.env.PORT || config.port;
if (config.analog) {
suffix = '?analog';
} else {
suffix = '';
}
app.get('/', function(req, res) {
if (config.useGamepadByDefault) {
return res.redirect('gamepad.html' + suffix);
} else {
return res.redirect('index.html' + suffix);
}
});
app.use(express["static"](__dirname + '/public'));
io.on('connection', function(socket) {
socket.on('disconnect', function() {
if (socket.gamePadId !== void 0) {
log('info', 'Gamepad disconnected');
return gp_hub.disconnectGamepad(socket.gamePadId, function() {});
} else if (socket.keyBoardId !== void 0) {
log('info', 'Keyboard disconnected');
return kb_hub.disconnectKeyboard(socket.keyBoardId, function() {});
} else if (socket.touchpadId !== void 0) {
log('info', 'Touchpad disconnected');
return tp_hub.disconnectTouchpad(socket.touchpadId, function() {});
} else {
return log('info', 'Unknown disconnect');
}
});
socket.on('connectGamepad', function() {
return gp_hub.connectGamepad(function(gamePadId) {
var ledBitField;
ledBitField = config.ledBitFieldSequence[gamePadId];
if (gamePadId !== -1) {
log('info', 'connectGamepad: success');
socket.gamePadId = gamePadId;
return socket.emit('gamepadConnected', {
padId: gamePadId,
ledBitField: ledBitField
});
} else {
return log('warning', 'connectGamepad: failed');
}
});
});
socket.on('padEvent', function(data) {
log('debug', 'padEvent ' + JSON.stringify(data));
if (socket.gamePadId !== void 0 && data) {
return gp_hub.sendEvent(socket.gamePadId, data);
}
});
socket.on('connectKeyboard', function() {
return kb_hub.connectKeyboard(function(keyBoardId) {
if (keyBoardId !== -1) {
log('info', 'connectKeyboard: success');
socket.keyBoardId = keyBoardId;
return socket.emit('keyboardConnected', {
boardId: keyBoardId
});
} else {
return log('info', 'connectKeyboard: failed');
}
});
});
socket.on('boardEvent', function(data) {
log('debug', 'boardEvent ' + JSON.stringify(data));
if (socket.keyBoardId !== void 0 && data) {
return kb_hub.sendEvent(socket.keyBoardId, data);
}
});
socket.on('connectTouchpad', function() {
return tp_hub.connectTouchpad(function(touchpadId) {
if (touchpadId !== -1) {
log('info', 'connectTouchpad: success');
socket.touchpadId = touchpadId;
return socket.emit('touchpadConnected', {
touchpadId: touchpadId
});
} else {
return log('info', 'connectTouchpad: failed');
}
});
});
return socket.on('touchpadEvent', function(data) {
log('debug', 'touchpadEvent ' + JSON.stringify(data));
if (socket.touchpadId !== void 0 && data) {
return tp_hub.sendEvent(socket.touchpadId, data);
}
});
});
http.on('error', function(err) {
if (err.hasOwnProperty('errno')) {
switch (err.errno) {
case "EACCES":
log('error', "You don't have permissions to open port " + port + ". " + "For ports smaller than 1024, you need root privileges.");
}
}
throw err;
});
http.listen(port, function() {
return log('info', "Listening on " + port);
});
}).call(this);