-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
96 lines (76 loc) · 3.08 KB
/
server.js
File metadata and controls
96 lines (76 loc) · 3.08 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
// a simple variable will store the GCM-ID for now, will be replaced with a database in the future
var tempGcmId = null;
var bodyParser = require('body-parser')
var express = require('express');
var app = express();
var gcm = require('node-gcm');
var config = require('./config');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// print the current GCM-Id to the browser
app.get('/', function(req, res) {
if (tempGcmId == null) {
res.send('Id not set! Just run the app and reload the page. You should see a GCM-id after that.');
} else {
res.send('Id is ' + tempGcmId);
}
});
// set the current GCM-ID (will override the old one)
app.post('/setGcmId', function(req, res) {
if (req.body.secret != config.auth.secret)
{
res.statusCode = 403;
res.send('The parameter "secret" contains either none or an invalid secret. The GCM-id was not set because the authentification failed.');
return;
}
tempGcmId = req.body.gcmId;
res.send('Id set: ' + tempGcmId);
});
app.post('/ring', function(req, res) {
// check if a GCM-client id is known to the server.
// The BigRedButton-App will tell the server the GCM-id via the route /setGcmId and only after this happened
// the /ring-action can be executed successfully. Currently the GCM-ID will be lost if the server is restarted, because it's not stored persistent.
if (tempGcmId == null) {
// internal server error
res.statusCode = 500;
res.send('Id not set!');
return;
}
if (req.body.secret != config.auth.secret)
{
res.statusCode = 403;
res.send('The parameter "secret" contains either none or an invalid secret. The alarm is not fired because the authentification failed.');
return;
}
// Preparing the GCM message which will be delivered to the device via Google Servers
// Look here for more information on the parameters: https://github.com/ToothlessGear/node-gcm
var message = new gcm.Message({
priority: 10,
timeToLive: 0,
delayWhileIdle: false
});
// It is required to obtain a API-Key from Google to use GCM.
// More information are available here: https://support.google.com/googleplay/android-developer/answer/2663268
var sender = new gcm.Sender(config.gcm.apikey);
// Adding the registration IDs of the devices we want to send to
// The alarm could possibilty go to more than one device, but at the current stage we are sending it
// to exactly one.
var registrationIds = [];
registrationIds.push(tempGcmId);
// Send the message
sender.send(message, registrationIds, function(err, result) {
if (err) {
console.error(err);
// Oops... something went wrong!
res.statusCode = 500; // internal server error
res.send(err);
} else {
res.statusCode = 200; // OK
console.log(result);
res.send(result);
}
});
});
// TODO => create a config file to configure server-port etc.
app.listen(config.port);