-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
87 lines (74 loc) · 3.1 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
76
77
78
79
80
81
82
83
84
85
86
87
var Room = require('./room');
var config = require('./config/config.json');
var baseUrl = config.base_url;
var timesyncServer = require('timesync/server');
var express = require('express');
var app = express();
var expressLayouts = require('express-ejs-layouts');
var tracks = require('./tracks.json');
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.set('port',(process.env.PORT||3000));
http.listen (app.get('port'),function() {
console.log("Listening to port number " + app.get('port'));
});
var links = ["demoal","specry","credry","phonal","tactly","nating","fracor","medish","logive","duceur","tactly",
"fratee","medent","logish","cepess","vokous","biblic","verant","malish","pedous","loging","mitish",
"tenian","regant","scriby","nomman","cyclee","exlegy","venive","geneur","marure","verter","tensly",
"cluser","bioics","tainic","geoish","tenish","venure","ferity","fracer","rectly","nateur","legous",
"capess","dictty","pedman","tenent","mining","domian"];
Room.io = io;
app.use(express.static(__dirname + '/public'));
app.use(expressLayouts);
app.use('/timesync', timesyncServer.requestHandler);
app.set('layout');
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.get('/', function(req, res) {
res.render('index');
});
app.get('/create/room', function(req, res) {
res.render('room');
});
app.get('/join/room', function(req, res) {
res.render('joiner');
});
app.get('/room/:room', function (req, res) {
res.render('room');
});
var playerInstances = "";
io.on('connection', function(socket) {
console.log(Room.isStartTime);
console.log("User Connected");
var x = Math.floor(Math.random() * 50);
var myRoom = links[x];
var room;
socket.on('startSetter', function(msg) {
console.log("Socket recieved");
if (!Room.isStartTime) {
Room.isStartTime = true;
Room.startTime = msg;
}
console.log(Room.startTime);
socket.emit("start", Room.startTime);
});
socket.emit("roomAll", playerInstances);
if (socket.handshake.headers.referer.split('/')[3] == 'create') {
if(socket.handshake.headers.referer.split('/')[5] == undefined) {
room = Room.allocateFirst(socket,myRoom);
var selector = Math.floor(Math.random() * tracks.count);
room.selectedTrack = String(tracks.ids[selector]);
console.log("Track set = " + tracks.ids[selector]);
socket.emit('songId', room.selectedTrack);
socket.emit('alertLink', baseUrl + "/room/" + myRoom);
playerInstances = playerInstances + " " + baseUrl + "/room/" + myRoom;
console.log("New room allocated");
}
} else if (socket.handshake.headers.referer.split('/')[3] == 'room') {
room = Room.allocateOther(socket, socket.handshake.headers.referer.split('/')[4]);
socket.emit('songId', room.selectedTrack);
console.log("Room '" + socket.handshake.headers.referer.split('/')[4] + "' joined" );
} else {
console.log(socket.handshake.headers.referer);
}
});