-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·134 lines (104 loc) · 3.11 KB
/
app.js
File metadata and controls
executable file
·134 lines (104 loc) · 3.11 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
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
var express = require('express')
, http = require('http')
, path = require('path');
var app = express();
var logger = require('morgan');
var bodyParser = require('body-parser');
app.set('port', process.env.PORT || 3000)
app.set('views', __dirname + '/views')
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.use(express.static(path.join(__dirname, 'public')))
app.get('/', function (req, res) {
res.render('index')
});
app.get('/table', function (req, res) {
res.render('table')
});
app.get('/device', function (req, res) {
res.render('device')
});
app.get('/rainy', function (req, res) {
res.render('rainy')
});
app.get('/snowy', function (req, res) {
res.render('snowy')
});
app.get('/cloudy', function (req, res) {
res.render('cloudy')
});
app.get('/2016', function (req, res) {
res.render('2016')
});
app.get('/medical', function (req, res) {
res.render('medical')
});
app.get('/mehtap', function (req, res) {
res.render('mehtap')
});
app.use(notFoundHandler);
app.use(errorHandler);
function notFoundHandler(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err)
}
function errorHandler(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {error: err, errorStatus: err.status})
}
// Enable Socket.io
var server = http.Server(app);
var io = require('socket.io')(server);
// object to store table sockets
var tableSockets = {};
// A user connects to the server (opens a socket)
io.on('connection', function (socket) {
console.log("a new device connected: " + socket.id);
// device disconnected
socket.on('disconnect', function () {
console.log('device disconnected: ' + socket.id);
// if it's a table
if (socket.tableId) {
// remove table socket
delete tableSockets[socket.tableId];
}
});
// receives a connect message from the card table
socket.on('table-connect', function (tableId) {
// ... and stores the card table socket
console.log('table connected: ' + tableId);
tableSockets[tableId] = socket;
socket.tableId = tableId;
});
// receives a connect message from a phone
socket.on('phone-connect', function (tableId) {
var tableSocket = tableSockets[tableId];
if (tableSocket) {
// ... informs table that a phone has connected
tableSocket.emit('phone-connect');
}
});
// receives a move from a phone
socket.on('phone-move', function (data) {
var tableSocket = tableSockets[data.tableId];
if (tableSocket) {
// ... and forwards the current angle to the card table
tableSocket.emit('phone-move', data.angle);
}
});
// receives a throw card message from a phone
socket.on('phone-throw-card', function (data) {
var tableSocket = tableSockets[data.tableId];
if (tableSocket) {
// ... and forwards the data to the card table
tableSocket.emit('phone-throw-card', data);
}
});
});
server.listen(app.get('port'), function () {
console.log('Server listening at port: ' + app.get('port'))
});