-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
166 lines (140 loc) · 4.37 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
server
**/
var express = require('express');
var request = require('request');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var playList = new Array();
var histryList = new Array();
var nowVideo = null;
var isLoop = true;
var nowVolume=50;
// wwwディレクトリを静的ファイルディレクトリとして登録
app.use(express.static('www'));
// サーバを開始
server.listen(process.env.PORT || 8081);
// 次の動画idをplayに送信する
function sendNextVideoId(){
io.emit('timeZero', '');
// 初回起動時は落とす
if(playList.length == 0 && nowVideo == null && isLoop == true) {
return;
}
if(playList.length==0){
// ToDo: histryListを消去する仕様に変更したら落ちるようになる
nowVideo = histryList[ Math.floor( Math.random() * histryList.length ) ]
isLoop = true;
} else {
isLoop = false;
nowVideo = playList.shift()
histryList.push(nowVideo);
}
io.emit('nextVideoId', nowVideo.id);
io.emit('playList', {playList:playList, historyList:histryList, nowVideo:nowVideo});
}
// 動画の情報を取得する
function getVideoData(video){
var videoId;
var videoMatch = (video.match(/v=([^&]*)/) || video.match(/youtu\.be\/([^&]*)/));
if ( videoMatch ) {
videoId = videoMatch[1];
} else {
videoId = video;
}
var options = {
url: 'https://www.googleapis.com/youtube/v3/videos?' + 'id=' + videoId + '&key=' + 'AIzaSyCtpC85J9h0iGyr4YbpqkSrWloliGkXD6c' + '&part=' + 'snippet,contentDetails,statistics,status',
json: true
};
request.get(options, function (error, response, json) {
if (!error) {
console.log(json);
if(json.items.length > 0) {
playList.push(json.items[0]);
if(isLoop == true) {
console.log("isLoopがtrueだったから再生する");
sendNextVideoId();
}
}
io.emit('playList', {playList:playList, historyList:histryList});
console.log(playList);
}
});
}
io.on('connection', function (socket) {
// URLの追加処理
socket.on('add', function (msg) {
console.log(msg);
getVideoData(msg);
});
// コントローラ部分
socket.on('play', function(play){
io.emit('play', play);
});
socket.on('pause', function(pause){
io.emit('pause', pause);
});
socket.on('mute', function(mute){
io.emit('mute', mute);
});
socket.on('volumeOn', function(volume){
io.emit('volumeOn', volume);
});
socket.on('volumeChange', function(volume){
io.emit('volumeChange', volume);
nowVolume = volume;
});
// play側とコントローラ側からの次の動画要求
socket.on('next', function(play){
sendNextVideoId();
});
socket.on('up', function(up){
if( 0 < up && up < playList.length ){
var tmp = playList[parseInt(up)];
playList[parseInt(up)] = playList[parseInt(up)-1];
playList[parseInt(up)-1] = tmp;
}
io.emit('playList', {playList:playList, historyList:histryList});
});
socket.on('down', function(down){
if( 0 <= parseInt(down) && parseInt(down) < playList.length-1 ){
var tmp = playList[parseInt(down)+1];
playList[parseInt(down)+1] = playList[parseInt(down)];
playList[parseInt(down)] = tmp;
}
io.emit('playList', {playList:playList, historyList:histryList});
});
socket.on('mostUp', function(up){
for ( var i = up; i > 0; i--) {
if( 0 < i && i < playList.length ){
var tmp = playList[parseInt(i)];
playList[parseInt(i)] = playList[parseInt(i)-1];
playList[parseInt(i)-1] = tmp;
}
}
io.emit('playList', {playList:playList, historyList:histryList, nowVideo:nowVideo});
});
socket.on('mostDown', function(down){
for ( var i = down, leni = playList.length; i < leni-1;i++) {
if( 0 <= parseInt(i) && parseInt(i) < playList.length-1 ){
var tmp = playList[parseInt(i)+1];
playList[parseInt(i)+1] = playList[parseInt(i)];
playList[parseInt(i)] = tmp;
}
}
io.emit('playList', {playList:playList, historyList:histryList, nowVideo:nowVideo});
});
socket.on('deletePlay', function(del){
playList.splice(del,1);
io.emit('playList', {playList:playList, historyList:histryList, nowVideo:nowVideo});
});
socket.on('deleteHistry', function(del){
histryList.splice(del,1);
io.emit('playList', {playList:playList, historyList:histryList, nowVideo:nowVideo});
});
socket.on('update', function(update){
io.emit('playList', {playList:playList, historyList:histryList, nowVideo:nowVideo});
io.emit('volumeChange', nowVolume);
});
});