forked from MakerFaireLA/sitePlanJungle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
265 lines (237 loc) · 11.1 KB
/
Copy pathserver.js
File metadata and controls
265 lines (237 loc) · 11.1 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
var env = require('node-env-file');
var express = require('express');
var app = express();
// https://medium.com/@rafaelvidaurre/managing-environment-variables-in-node-js-2cb45a55195f
var dotenv = require('dotenv'); // for reading environment variables from a local .env file
dotenv.load();
var options =
{
root: __dirname,
dotfiles: 'deny',
headers:
{
'x-timestamp': Date.now(),
'x-sent': true
}
};
app.get('/', function(request, response, next) {
response.sendFile('index.html', options, function(err) {
if(err) {
next(err);
} else {
console.log('Sent: index.html');
}
});
});
app.use(express.static('public'));
// Setup the database
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
MongoClient.connect(process.env.MONGODB_URI, function (err, db) {
if (err) throw err;
// WebSocket Portion
// WebSockets work with the HTTP Server
// Putting back the WebSockets method using instructions in
// http://stackoverflow.com/questions/21365044/cant-get-socket-io-js
var port = process.env.PORT || 8080;
var server = app.listen(port, function () {
console.log('Server started on port ' + port);
});
var io = require('socket.io').listen(server);
// ===============================================
// Register a callback function to run when we have an individual connection.
// This is run for each individual user that connects.
io.sockets.on('connection', function(socket) {
console.log("We have a new client: " + socket.id);
// ------------------------------------------------
// Info sent on the 'broadcast' channel will be rebroadcast to all other clients
// in order that all users remain in sync.
socket.on('broadcast', function(data) {
// 'broadcast' channel listener
// supports all CRUD operations
if(!('op' in data)) {
console.log("Error: 'broadcast' => data received with unspecified operation (no op found).");
// @TODO - Should probably throw an error here if the op param is not found in data.
// @TODO - Need to check that value of 'op' is 'c', 'r', 'u', 'l' or 'd' and if not, throw an error.
// Or perhaps I should just use a switch statement and have the default throw an error.
} else if(data.op == 'u') {
// ------------------------------
// Update operation implementation
console.log("Received: 'broadcast' => update tile_id " + data.tile_id + " to " + data.x + " " + data.y
+ " and theta " + data.theta + " etc...");
db.collection(process.env.MONGODB_COLLECTION).updateOne(
{ "tile_id": data.tile_id },
{
$set: {
"location": {
'x': data.x,
'y': data.y
},
"dimensions": {
'x': data.dimx,
'y': data.dimy
},
"theta": data.theta,
"color": data.color,
"userRef": data.userRef,
"userLabel": data.userLabel
}
}, function (err) {
if (err) {
console.log("Error: Unable to update database for tile_id " + data.tile_id);
throw err;
} else {
// Send it to all other clients
socket.broadcast.emit('broadcast', data);
}
});
} else if(data.op == 'l') {
// ------------------------------
// Location-only update operation implementation
console.log("Received: 'broadcast' => update (op = l) tile_id " + data.tile_id + " to " + data.x + " " + data.y);
db.collection(process.env.MONGODB_COLLECTION).updateOne(
{ "tile_id": data.tile_id },
{
$set: {
"location": {
'x': data.x,
'y': data.y
}
}
}, function (err) {
if (err) {
console.log("Error: Unable to update database for tile_id " + data.tile_id);
throw err;
} else {
// Send it to all other clients
socket.broadcast.emit('broadcast', data);
}
});
} else if(data.op == 'a') {
// ------------------------------
// Angle-only update operation implementation
console.log("Received: 'broadcast' => update (op = a) tile_id " + data.tile_id + " to theta " + data.theta);
db.collection(process.env.MONGODB_COLLECTION).updateOne(
{ "tile_id": data.tile_id },
{
$set: {
'theta': data.theta
}
}, function (err) {
if (err) {
console.log("Error: Unable to update database for tile_id " + data.tile_id);
throw err;
} else {
// Send it to all other clients
socket.broadcast.emit('broadcast', data);
}
});
} else if(data.op == 'c') {
// ------------------------------
// Create operation implementation
console.log("Received: 'broadcast' => create tile_id " + data.tile_id + " at " + data.location.x + " "
+ data.location.y + " with theta " + data.theta + " etc...");
db.collection(process.env.MONGODB_COLLECTION).insertOne(
{'tile_id': data.tile_id, 'location':{'x': data.location.x, 'y':data.location.y}, 'theta': data.theta,
'dimensions':{'x': data.dimensions.x, 'y': data.dimensions.y}, 'color': data.color,
'userRef': data.userRef, 'userLabel': data.userLabel},
function(err) {
if(err) {
console.log("Error: Unable to insert new tile in database for tile_id " + data.tile_id);
throw err;
} else {
// Send it to all other clients
socket.broadcast.emit('broadcast', data);
}
});
} else if(data.op == 'd') {
// ------------------------------
// Delete operation implementation
console.log("Received: 'broadcast' => delete tile_id " + data.tile_id);
db.collection(process.env.MONGODB_COLLECTION).deleteOne(
{'tile_id': data.tile_id},
function(err) {
if(err) {
console.log("Error: Failed to delete tile_id " + data.tile_id + " from database.");
throw err;
} else {
// rebroadcast to all other clients
socket.broadcast.emit('broadcast', data);
}
});
} else if(data.op == 'r') {
// ------------------------------
// Read operation implementation
console.log("Received: 'broadcast' => read tile_id " + data.tile_id);
db.collection(process.env.MONGODB_COLLECTION).findOne(
{'tile_id': data.tile_id},
function(err, doc) {
if(err) {
console.log("Error: Read op failed to find tile_id " + data.tile_id + " in database.");
throw err;
} else {
var update = {
op: 'u',
tile_id: data.tile_id,
x: data.x,
y: data.y,
dimx: data.dimx,
dimy: data.dimy,
theta: data.theta,
color: data.color,
userRef: data.userRef,
userLabel: data.userLabel
};
socket.emit('broadcast', update);
}
});
}
});
// ------------------------------------------------
// 'server' channel listener
// Handles non-CRUD requests (that are not to be re-broadcast)
// These are generally client->server only.
socket.on('server', function(message) {
console.log("Received: 'server' => request for initialization on all tiles.");
db.collection(process.env.MONGODB_COLLECTION).find({ 'tile_id': { $exists: true } }).toArray(function (err, res) {
if(err) {
console.log("Error: failed to retrieve tile data from database, will not be able to initialize client.");
throw err;
} else {
for (var key in res) {
sendTileInitData(socket, res[key].tile_id, res[key].location.x, res[key].location.y,
res[key].dimensions.x, res[key].dimensions.y, res[key].theta, res[key].color,
res[key].userRef, res[key].userLabel);
}
}
});
});
// ------------------------------------------------
socket.on('disconnect', function () {
console.log("Client has disconnected");
});
});
});
// ===============================================
// Send client tile initialization data
function sendTileInitData(tunnel, tile_id_Arg, xArg, yArg, dimxArg, dimyArg, thetaArg, colorArg, userRefArg, userLabelArg) {
console.log("Send 'broadcast' => initialize (op = 'c') tile_id " + tile_id_Arg + " at " + xArg + " " + yArg
+ " with theta " + thetaArg + " etc...");
var data = {
'op': 'c',
'tile_id': tile_id_Arg,
'location': {
'x': xArg,
'y': yArg
},
'dimensions': {
'x': dimxArg,
'y': dimyArg
},
'theta': thetaArg,
'color': colorArg,
'userRef': userRefArg,
'userLabel': userLabelArg
};
tunnel.emit('broadcast', data);
}