-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (126 loc) · 4.21 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var express = require("express");
var app = express();
var http = require('http'),
socketio = require('socket.io'),
fs = require("fs"),
config = require('config');
//configs
var photosHost = config.get('photosHost');
var videosHost = config.get('videosHost');
var photosPath = config.get('photos_path');
app.set('views', __dirname + '/app/views');
app.use(express.static(__dirname + '/app'));
app.engine('html', require('ejs').renderFile);
app.get('/', function(request, response) {
response.render('index.html');
});
app.get('/room/:room_name', function(request, response){
response.render('index.html');
})
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});
var io = require('socket.io'),
http = require('http'),
server = http.createServer(app),
io = io.listen(server);
var users = [];
function delUser(id){
var username;
for(room in users){
for(var j=0;j<users[room].length;j++){
if(users[room][j].id == id){
username = users[room][j].username;
users[room].splice(j,1);
return {room: room, username: username};
}
}
}
return null;
}
function getUserIdByRoom(room, id){
for(var j=0;j<users[room].length;j++){
if(users[room][j].id == id){
return j;
}
}
return null;
}
function getUsernames(room){
if(!users[room]) return [];
names = [];
for(var j=0;j<users[room].length;j++){
names.push(users[room][j].username);
}
return names;
}
function saveImgFromBase64(name, base64){
var matches = base64.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
response = {};
if (matches.length !== 3) {
return new Error('Invalid input string');
}
response.type = matches[1];
response.data = new Buffer(matches[2], 'base64');
name = name+'_'+process.hrtime()+'.png';
fs.writeFile(photosPath+name, response.data, function(err) {});
return photosHost+name;
}
var videos = ['vid1.mp4', 'vid2.mp4'];
function getRandVideo() {
return videosHost + videos[Math.floor(Math.random() * videos.length)]
}
io.on('connection', function (socket) {
socket.on('join', function(data){
var room = data.room;
socket.join(room);
if(!users[room]){
users[room] = [];
}
users[room].push({id: socket.id, username: data.username, video_id: data.video_id, watched_videos: 0});
//io.to(room).emit('room', data.username + ' joined');
console.log('new user: '+data.username);
console.log(users);
io.to(room).emit('users', {new_user: data.username, users: users[room]});
});
socket.on('disconnect', function(){
var del = delUser(this.id);
if(!del) return;
io.to(del.room).emit('users', {users: users[del.room], user_left: del.username});
console.log(del.username + ' left from room ' + del.room);
console.log(users)
});
socket.on('chat', function(data){
io.to(data.room).emit('room', data);
});
socket.on('cancel_video', function(data){
console.log(data.username + ' canceled video');
var id = getUserIdByRoom(data.room, socket.id);
data.victim_video = users[data.room][id].video_id;
io.to(data.room).emit('cancel_video', data);
});
socket.on('video_ended', function(data){
console.log(data.username + ' watched video');
var room = data.room;
var id = getUserIdByRoom(room, socket.id);
users[room][id].watched_videos = users[room][id].watched_videos + 1;
data.victim_video = users[room][id].video_id;
io.to(room).emit('video_ended', data);
io.to(room).emit('users', {users: users[room]});
});
socket.on('roulette', function(data){
console.log('roulette from room ' + data.room);
var rand = Math.floor(Math.random() * users[data.room].length);
data.victim = users[data.room][rand].username;
data.victim_video = users[data.room][rand].video_id;
data.video = getRandVideo();
io.to(data.room).emit('roulette', data);
});
// data = {takenby: '', username: '', photo: '', room}
socket.on('photo', function(data){
data.photoUrl = saveImgFromBase64(data.username, data.photo);
io.to(data.room).emit('photo', data);
});
});
server.listen(8080);