forked from MaikePaetzel/1md031_18_students
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (58 loc) · 1.97 KB
/
app.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
/*jslint node: true */
/* eslint-env node */
'use strict';
// Require express, socket.io, and vue
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
// Pick arbitrary port for server
var port = 3000;
app.set('port', (process.env.PORT || port));
// Serve static assets from public/
app.use(express.static(path.join(__dirname, 'public/')));
// Serve vue from node_modules as vue/
app.use('/vue', express.static(path.join(__dirname, '/node_modules/vue/dist/')));
// Serve index.html directly as root page
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'views/index.html'));
});
// Serve map.html as /map
app.get('/map', function (req, res) {
res.sendFile(path.join(__dirname, 'views/map.html'));
});
// Serve dispatcher.html as /dispatcher
app.get('/dispatcher', function (req, res) {
res.sendFile(path.join(__dirname, 'views/dispatcher.html'));
});
// Store data in an object to keep the global namespace clean and
// prepare for multiple instances of data if necessary
function Data() {
this.orders = {};
}
/*
Adds an order to to the queue
*/
Data.prototype.addOrder = function (order) {
//Store the order in an "associative array" with orderId as key
this.orders[order.orderId] = order;
};
Data.prototype.getAllOrders = function () {
return this.orders;
};
var data = new Data();
io.on('connection', function (socket) {
// Send list of orders when a client connects
socket.emit('initialize', { orders: data.getAllOrders() });
// When a connected client emits an "addOrder" message
socket.on('addOrder', function (order) {
console.log(order);
data.addOrder(order);
// send updated info to all connected clients, note the use of io instead of socket
io.emit('currentQueue', { orders: data.getAllOrders() });
});
});
var server = http.listen(app.get('port'), function () {
console.log('Server listening on port ' + app.get('port'));
});