-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathex4.html
More file actions
430 lines (380 loc) · 12.8 KB
/
ex4.html
File metadata and controls
430 lines (380 loc) · 12.8 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<!DOCTYPE>
<head>
<meta charset='utf-8'>
<title>ex4</title>
<style>
body {font-family:Arial, Helvetica, sans-serif}
div {padding:3px}
#control {background-color:beige;font-size:0.8em}
#param {background-color:wheat;font-size:11px}
#param * {font-size:10px}
#url, #messages {font-size:0.8em;font-family:'Courier New', Courier, monospace}
li.open, li.close {color:blue}
li.command {color:orange}
li.error {color:red}
</style>
</head>
<body>
<h2>예제4. 녹화 영상 받아내기 (Web Socket)</h2>
<div id='control'>
<div>
<input type='text' id='host-name' placeholder='서버 IP주소:포트'>
<input type='text' id='user-id' placeholder='사용자 ID'>
<input type='password' id='password' placeholder='비밀번호'>
</div>
<div id='param'>
<div>
데이터 구간 : <input type='datetime-local' id='timeBegin' step='1' value='2018-07-27T09:00:00'>
~ <input type='datetime-local' id='timeEnd' step='1' value='2018-07-27T09:30:00'>
</div>
<div>
채널:
<input type='checkbox' onclick='onSelectAllChannels(this)'>모두 선택
<input type='checkbox' class='chid' value='1' checked>1
<input type='checkbox' class='chid' value='2'>2
<input type='checkbox' class='chid' value='3'>3
<input type='checkbox' class='chid' value='4'>4
<input type='checkbox' class='chid' value='5'>5
<input type='checkbox' class='chid' value='6'>6
<input type='checkbox' class='chid' value='7'>7
<input type='checkbox' class='chid' value='8'>8
<input type='checkbox' class='chid' value='9'>9
<input type='checkbox' class='chid' value='10'>10
<input type='checkbox' class='chid' value='11'>11
<input type='checkbox' class='chid' value='12'>12
<input type='checkbox' class='chid' value='13'>13
<input type='checkbox' class='chid' value='14'>14
<input type='checkbox' class='chid' value='15'>15
<input type='checkbox' class='chid' value='16'>16
</div>
<div>
파일 크기: <input type='text' id='mediaSize' placeholder='예) 500MB'>
</div>
<div>
자막 형식: <select id='subtitleFormat'>
<option value='SRT' selected>SRT</option>
<option value='SMI'>SMI</option>
<option value='VTT'>VTT</option>
<option value='None'>사용안함</option>
</select>
</div>
<div>
제출자: <input type='text' id='submitter' placeholder='예) 흥부'>
</div>
<div>
수령인: <input type='text' id='recipient' placeholder='예) 놀부'>
</div>
<div>
용도: <textarea cols='40' rows='3' id='purpose' placeholder='예) 너무 배고파서...'></textarea>
</div>
<div>
진행 표시 주기: <input type='text' id='statusInterval' placeholder='예) 2s' value='2s'>
</div>
<div>
언어: <select id='lang'>
<option value='ko-KR'>한국어</option>
<option value='en-US'>영어</option>
<option value='es-ES'>스페인어</option>
<option value='zh-CN'>중국어 (간체)</option>
<option value='zh-TW'>중국어 (번체)</option>
</select>
</div>
<div>
<input type='checkbox' id='md5' value='md5'>md5
</div>
<div>
압축 파일 비밀번호: <input type='password' id='zipPassword' value=''>
</div>
</div>
<div>
<button type='button' onClick='onConnect()'>접속</button>
<button type='button' onClick='onDisconnect()'>접속 종료</button>
<button type='button' onClick='onClearAll()'>모두 삭제</button>
<button type='button' onClick='onCancel()' id='cancel' style='visibility:hidden;color:red'>작업 취소</button>
</div>
<div id='url'>
</div>
</div>
<div>
<ul id='messages'></ul>
</div>
</body>
<script type='text/javascript'>
(function() {
window.myApp = {
ws: null,
task: '',
fname: '',
auth: '',
downloadJobs: []
};
})();
function getURL() {
var url = '';
if (typeof(WebSocket) === 'undefined') {
alert('웹 소켓을 지원하지 않는 웹 브라우저입니다.');
return url;
}
if(window.myApp.ws !== null) {
alert('이미 접속 중입니다.');
return url;
}
var hostName = document.getElementById('host-name').value;
if(hostName == '') {
alert('호스트를 입력하십시오.');
return url;
}
var userId = document.getElementById('user-id').value;
if(userId == '') {
alert('사용자 아이디를 입력하십시오.');
return url;
}
var password = document.getElementById('password').value;
if(password == '') {
alert('비밀번호를 입력하십시오.');
return url;
}
// 매개 변수들
var timeBegin = document.getElementById('timeBegin').value;
if(timeBegin == '') {
alert('데이터 구간을 입력하십시오.');
return url;
}
var timeEnd = document.getElementById('timeEnd').value;
if(timeEnd == '') {
alert('데이터 구간을 입력하십시오.');
return url;
}
var ch = '';
var chk = document.getElementsByClassName('chid');
for (var i = 0; i < chk.length; i++) {
if (chk[i].checked === true) {
if(ch.length > 0)
ch += ',';
ch += chk[i].value;
}
}
if(ch.length == 0) {
alert('채널을 선택하십시오.');
return url;
}
var mediaSize = document.getElementById('mediaSize').value;
if(mediaSize == '') {
alert('동영상 파일의 최대 크기를 입력하십시오.');
return url;
}
var subtitleFormat = document.getElementById('subtitleFormat').value;
if(subtitleFormat == '') {
alert('자막 형식을 선택하십시오.');
return url;
}
var submitter = document.getElementById('submitter').value;
if(submitter == '') {
alert('제출자를 입력하십시오.');
return url;
}
var recipient = document.getElementById('recipient').value;
if(submitter == '') {
alert('수령인을 입력하십시오.');
return url;
}
var purpose = document.getElementById('purpose').value;
if(purpose == '') {
alert('용도를 입력하십시오.');
return url;
}
var statusInterval = document.getElementById('statusInterval').value;
if(statusInterval == '') {
alert('진행 표시 주기를 입력하십시오.');
return url;
}
var lang = document.getElementById('lang').value;
if(lang == '') {
alert('언어를 선택하십시오.');
return url;
}
var encodedData = window.btoa(userId + ':' + password); // base64 인코딩
window.myApp.auth = encodeURIComponent(encodedData);
url = (hostName.includes('ws://', 0) ? '' : 'ws://') +
hostName + '/wsapi/dataExport?auth=' + window.myApp.auth;
url +=
'&timeBegin=' + encodeURIComponent(timeBegin) +
'&timeEnd=' + encodeURIComponent(timeEnd) +
'&ch=' + encodeURIComponent(ch) +
'&subtitleFormat=' + encodeURIComponent(subtitleFormat) +
'&mediaSize=' + encodeURIComponent(mediaSize) +
'&statusInterval=' + encodeURIComponent(statusInterval) +
'&submitter=' + encodeURIComponent(submitter) +
'&recipient=' + encodeURIComponent(recipient) +
'&purpose=' + encodeURIComponent(purpose) +
'&lang=' + encodeURIComponent(lang);
var zipPassword = document.getElementById('zipPassword').value;
if (zipPassword)
url += '&password=' + encodeURIComponent(window.btoa(zipPassword));
var md5 = document.getElementById('md5');
if (md5.checked === true) {
url += '&md5=true';
}
return url;
}
function addItem(tagClass, msg) {
var li = document.createElement('li');
li.appendChild(document.createTextNode(msg));
li.classList.add(tagClass);
document.getElementById('messages').appendChild(li);
}
function addDownloadItem(fname) {
var li = document.createElement('li');
var span = document.createElement('span');
span.innerHTML = '<progress value="0" max="100"></progress> <label>' + fname + '<label>';
span.setAttribute('id', window.myApp.task + '/' + fname);
li.appendChild(span);
document.getElementById('messages').appendChild(li);
}
function showCancelButton(bShow) {
var el = document.getElementById('param');
el.style.display = bShow ? 'none' : 'block';
el = document.getElementById('cancel');
el.style.visibility = bShow ? 'visible' : 'hidden';
}
function onConnect() {
var url = getURL();
if(url.length == 0)
return;
document.getElementById('url').innerText = url;
// 웹 소켓 인스턴스와 핸들러 함수들
var ws = new WebSocket(url);
ws.onopen = function() {
addItem('open', '접속 성공');
};
ws.onclose = function(e) {
addItem('close', '접속 종료: ' + e.code);
onDisconnect();
};
ws.onerror = function(e) {
addItem('error', '오류: ' + e.code);
};
ws.onmessage = function(e) {
addItem('data', e.data);
var msg = JSON.parse(e.data);
switch(msg.stage) {
case 'ready':
if(msg.status.code != 0)
break;
window.myApp.task = msg.task.id;
showCancelButton(true);
break;
case 'fileBegin':
window.myApp.fname = msg.channel.file.name;
break;
case 'fileEnd':
downloadFiles(msg.channel.file, function(bSuccess) {
sendCommand(bSuccess ? "next" : "cancel");
});
break;
case 'timeoutAlert':
if( window.myApp.downloadJobs.length > 0)
sendCommand("wait");
break;
case 'end':
showCancelButton(false);
break;
}
};
window.myApp.ws = ws;
}
function onDisconnect() {
if(window.myApp.ws !== null) {
window.myApp.ws.close();
window.myApp.ws = null;
}
document.getElementById('param').style.display = 'block';
}
function onClearAll() {
var el = document.getElementById("messages");
while (el.firstChild)
el.removeChild(el.firstChild);
document.getElementById('url').innerText = '';
document.getElementById('param').style.display = 'block';
}
function downloadFiles(file, onFinished) {
if(file.download.length <= 0) {
onFinished(false);
return;
}
var downloadCnt = 0, successCnt = 0;
function onDone(bSuccess) {
if(bSuccess)
successCnt++;
if(++downloadCnt < file.download.length) {
setTimeout(function() {
downloadFile(file.download[downloadCnt], onDone);
}, 1000);
}
else {
onFinished(successCnt == downloadCnt);
}
}
downloadFile(file.download[downloadCnt], onDone);
}
function downloadFile(download, onFinish) {
addDownloadItem(download.fileName);
var req = new XMLHttpRequest();
window.myApp.downloadJobs.push(req);
// 다운로드 링크에 auth 매개변수를 붙여서 요청
req.open('GET', download.src + '?auth=' + window.myApp.auth, true);
req.responseType = "blob";
req.onreadystatechange = function() {
if (req.readyState === 4 && req.status === 200) {
var a = document.createElement('a');
a.href = window.URL.createObjectURL(req.response);
a.download = download.fileName;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(a.href);
}
},
req.onprogress = function(e) {
var prog = document.getElementById(window.myApp.task + '/' + download.fileName).firstChild;
if(prog)
prog.value = Math.ceil(e.loaded * 100 / e.total);
},
req.onerror = function(e) {
if(onFinish)
onFinish(false);
var pos = window.myApp.downloadJobs.indexOf(req);
if(pos >= 0)
window.myApp.downloadJobs.splice(pos, 1);
},
req.onloadend = function (event) {
var pos = window.myApp.downloadJobs.indexOf(req);
if(pos >= 0)
window.myApp.downloadJobs.splice(pos, 1);
if(onFinish)
onFinish(true);
};
req.send();
}
function sendCommand(command) {
var str = JSON.stringify({
task: window.myApp.task,
cmd: command
});
addItem('command', str)
window.myApp.ws.send(str);
}
function onCancel() {
// 다운로드 작업을 모두 중단
window.myApp.downloadJobs.forEach(function(jobs) {
jobs.abort();
});
window.myApp.downloadJobs = [];
sendCommand("cancel");
}
function onSelectAllChannels(el) {
var ch = document.getElementsByClassName('chid');
for(var i=0, cnt=ch.length; i<cnt; i++)
ch[i].checked = el.checked;
}
</script>