-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathserver.coffee
106 lines (90 loc) · 3.22 KB
/
server.coffee
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
###
Created by MIROOF on 04/03/2015
Virtual gamepad application
###
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
# Add URL query string if analog mode is enabled
if config.analog
suffix = '?analog'
else
suffix = ''
# draw routes
app.get '/', (req, res) ->
if config.useGamepadByDefault
res.redirect 'gamepad.html' + suffix
else
res.redirect 'index.html' + suffix
app.use(express.static(__dirname + '/public'));
# socket io
io.on 'connection', (socket) ->
socket.on 'disconnect', () ->
if socket.gamePadId != undefined
log 'info', 'Gamepad disconnected'
gp_hub.disconnectGamepad socket.gamePadId, () ->
else if socket.keyBoardId != undefined
log 'info', 'Keyboard disconnected'
kb_hub.disconnectKeyboard socket.keyBoardId, () ->
else if socket.touchpadId != undefined
log 'info', 'Touchpad disconnected'
tp_hub.disconnectTouchpad socket.touchpadId, () ->
else
log 'info', 'Unknown disconnect'
socket.on 'connectGamepad', () ->
gp_hub.connectGamepad (gamePadId) ->
ledBitField = config.ledBitFieldSequence[gamePadId]
if gamePadId != -1
log 'info', 'connectGamepad: success'
socket.gamePadId = gamePadId
socket.emit 'gamepadConnected', {padId: gamePadId, ledBitField: ledBitField}
else
log 'warning', 'connectGamepad: failed'
socket.on 'padEvent', (data) ->
log 'debug', 'padEvent '+ JSON.stringify(data)
if socket.gamePadId != undefined and data
gp_hub.sendEvent socket.gamePadId, data
socket.on 'connectKeyboard', () ->
kb_hub.connectKeyboard (keyBoardId) ->
if keyBoardId != -1
log 'info', 'connectKeyboard: success'
socket.keyBoardId = keyBoardId
socket.emit 'keyboardConnected', {boardId: keyBoardId}
else
log 'info', 'connectKeyboard: failed'
socket.on 'boardEvent', (data) ->
log 'debug', 'boardEvent '+ JSON.stringify(data)
if socket.keyBoardId != undefined and data
kb_hub.sendEvent socket.keyBoardId, data
socket.on 'connectTouchpad', () ->
tp_hub.connectTouchpad (touchpadId) ->
if touchpadId != -1
log 'info', 'connectTouchpad: success'
socket.touchpadId = touchpadId
socket.emit 'touchpadConnected', {touchpadId: touchpadId}
else
log 'info', 'connectTouchpad: failed'
socket.on 'touchpadEvent', (data) ->
log 'debug', 'touchpadEvent '+ JSON.stringify(data)
if socket.touchpadId != undefined and data
tp_hub.sendEvent socket.touchpadId, data
http.on 'error', (err) ->
if err.hasOwnProperty('errno')
switch err.errno
when "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, () ->
log 'info', "Listening on #{port}"