-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (64 loc) · 2.15 KB
/
index.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
76
77
var http = require("http");
var express = require("express");
var app = express();
// *--- force https ---*
//from : https://glitch.com/edit/#!/force-http-or-https?path=server.js
// app.set('trust proxy', true); // <- required
// app.use((req, res, next) => {
// if(!req.secure) return res.redirect('https://' + req.get('host') + req.url);
// next();
// });
//^--- but, this doesn't work :( for us.
// maybe... 'flyio' is interfering the middle (http://pzzz.ink <- flyio -> https://dianaband-paradezzz.glitch.me)
// ==> so, we won't do it. but we will let the client do it. -> public/sketch.js #7 ~ #11
var server = http.createServer(app);
var port = process.env.PORT || 9009;
server.listen(port);
//
app.use(express.static("public"));
var io = require("socket.io")(server, {
pingInterval: 1000,
pingTimeout: 3000
});
//
var score = require("./public/score.json");
//
//there will be 16 rooms called: "room0", "room1", ... , "room15"
//if any other room is requested.. well, we will simply reject.
var roommax = 16;
//
io.on("connection", function(socket) {
console.log("someone connected.");
socket.on("disconnect", function() { console.log("someone disconnected."); });
socket.on("room", function(room, fn) {
// parseInt(room)
if (room >= 0 && room < roommax) {
socket.join("room" + room);
fn(true);
} else {
fn(false);
}
});
});
//
var pointer = 0; // pointer : 0 ~ (length-1)
var looper;
(looper = function(timeout) {
setTimeout(function() {
//pointer = 20;
// console.log(score[pointer]);
//
for (var index = 0; index < roommax; index++) {
// NOTE: 'pointer' must be 'remembered' since 'pointer' will increase almost immediately! pass as argument => 'pointed'
// NOTE: 'index' is same => 'indexed'
setTimeout(function(pointed, indexed) {
io.to("room" + indexed).emit("post", score[pointed]);
}, score[pointer].object.showtime * index, pointer, index);
}
var timegap = score[pointer].timegap.base + Math.random()*score[pointer].timegap.random;
// console.log(timegap);
pointer++;
if (pointer >= score.length) pointer = 0;
looper(timegap);
}, timeout);
})(1000);