-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
75 lines (63 loc) · 2.04 KB
/
server.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
71
72
73
74
75
const express = require("express");
const bodyParser = require("body-parser");
const randomWords = require('random-words');
const channeName = "ably-data-server";
require('dotenv').config()
const key = process.env.API_KEY;
const rest = new require("ably").Rest(key);
const channel = rest.channels.get(channelName);
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/index.html");
});
// tokenrequest based authentication
app.get("/auth", (req, res) => {
const tokenParams = {
ttl: 1000 * 60 * 60 * 1, // one hour
clientId: "[email protected]",
};
rest.auth.createTokenRequest(tokenParams, (err, tokenRequest) => {
if (err) {
console.error("Bang");
res
.status(500)
.send("Error requesting TokenRequest: " + JSON.stringify(err));
} else {
let timestamp = new Date().toLocaleString();
console.log("Token request issued at: " + timestamp);
console.log(tokenRequest);
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify(tokenRequest));
}
});
});
const max = 4; // add or remove up to this number of users
const initialUsers = 50;
let usernames = randomWords(initialUsers);
function addUsers(users) {
for (let i = 0; i < Math.floor(Math.random() * max) + 1; i++) {
users.push(randomWords());
}
}
function removeUsers(users) {
for (let i = 0; i < Math.floor(Math.random() * max) + 1; i++) {
users.splice(Math.floor(Math.random() * users.length), 1);
}
}
function onlineUsernames(usernames) {
removeUsers(usernames);
addUsers(usernames);
console.log(usernames);
console.log('Length: ' + usernames.length);
return usernames;
}
function sendUserList() {
channel.publish("userlist", onlineUsernames(usernames));
}
const listener = app.listen(process.env.PORT, () => {
console.log("Your app is listening on port " + listener.address().port);
setInterval(sendUserList, process.env.INTERVAL);
});