質問編集履歴

1

コード全文の記載

2019/08/11 08:43

投稿

yuhei_seguchi
yuhei_seguchi

スコア13

test CHANGED
File without changes
test CHANGED
@@ -71,3 +71,583 @@
71
71
  自分は、'A'という文字列だけ取得したいのですが、中々上手くいきません。
72
72
 
73
73
  解決策がわかる方がいらしたら、教えていただけると嬉しいです。
74
+
75
+
76
+
77
+ ### コード全文
78
+
79
+ タイマー機能付きのweb通話アプリを作っています。
80
+
81
+ ```html
82
+
83
+ <!DOCTYPE html>
84
+
85
+ <html>
86
+
87
+ <head lang="ja">
88
+
89
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
90
+
91
+ <title>SkyWay JS SDK Tutorial</title>
92
+
93
+ <link rel="stylesheet" href="style.css">
94
+
95
+ </head>
96
+
97
+ <body>
98
+
99
+ <div class="pure-g">
100
+
101
+
102
+
103
+ <!-- Video area -->
104
+
105
+ <div class="pure-u-2-3" id="video-container">
106
+
107
+ <video id="their-video" autoplay></video>
108
+
109
+ <video id="my-video" muted="true" autoplay></video>
110
+
111
+ </div>
112
+
113
+
114
+
115
+ <!-- Steps -->
116
+
117
+ <div class="pure-u-1-3">
118
+
119
+ <h2>SkyWay Video Chat</h2>
120
+
121
+
122
+
123
+ <p>Your id: <span style="color: rgb(28, 184, 65);" id="my-id">...</span></p>
124
+
125
+ <p>Share this id with others so they can call you.</p>
126
+
127
+ <h3>Make a call</h3>
128
+
129
+ <form id="make-call" class="pure-form">
130
+
131
+ <input type="text" placeholder="Call user id..." id="callto-id">
132
+
133
+ <button href="#" class="pure-button pure-button-success" type="submit">Call</button>
134
+
135
+ </form>
136
+
137
+ <form id="end-call" class="pure-form">
138
+
139
+ <p>Currently in call with <span id="their-id"></span></p>//ここのspanタグ内のテキストを取得したい
140
+
141
+ <button href="#" class="pure-button pure-button-success" type="submit">End Call</button>
142
+
143
+ </form>
144
+
145
+ <div id="countDownWrap">
146
+
147
+ <div id="countDown"></div>
148
+
149
+ <ul>
150
+
151
+ <li id="startBtn"><img src="./img/btn_start.png" alt="スタート"></li>
152
+
153
+ <li id="stopBtn"><img src="./img/btn_stop.png" alt="ストップ"></li>
154
+
155
+ <li id="resetBtn"><img src="./img/btn_reset.png" alt="リセット"></li>
156
+
157
+ </ul>
158
+
159
+ <div id="controll">
160
+
161
+ <p>秒数変更<input type="text" id="setSecond"><img src="./img/btn_save.png" id="changeSecond"></p>
162
+
163
+ <p id="error"></p>
164
+
165
+ </div>
166
+
167
+ </div>
168
+
169
+ </div>
170
+
171
+ </div>
172
+
173
+ <!-- The core Firebase JS SDK is always required and must be listed first -->
174
+
175
+ <script type="text/javascript" src="https://www.gstatic.com/firebasejs/6.3.5/firebase-app.js"></script>
176
+
177
+ <script type="text/javascript" src="https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js"></script>
178
+
179
+ <script type="text/javascript" src="./config.js"></script>
180
+
181
+ <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
182
+
183
+ <script type="text/javascript" src="https://cdn.webrtc.ecl.ntt.com/skyway-latest.js"></script>
184
+
185
+ <script type="text/javascript" src="script.js"></script>
186
+
187
+
188
+
189
+ <!-- TODO: Add SDKs for Firebase products that you want to use
190
+
191
+ https://firebase.google.com/docs/web/setup#config-web-app -->
192
+
193
+ ```
194
+
195
+
196
+
197
+ ```javascript
198
+
199
+ 'use strict';
200
+
201
+
202
+
203
+ var db = firebase.firestore();
204
+
205
+ db.settings({
206
+
207
+ timestampsInSnapshots: true
208
+
209
+ });
210
+
211
+
212
+
213
+ let localStream = null;
214
+
215
+ let peer = null;
216
+
217
+ let existingCall = null;
218
+
219
+
220
+
221
+ // カメラ映像、マイク音声の取得
222
+
223
+ navigator.mediaDevices.getUserMedia({video: true, audio: true})
224
+
225
+ .then(function (stream) {
226
+
227
+ // Success
228
+
229
+ $('#my-video').get(0).srcObject = stream;
230
+
231
+ localStream = stream;
232
+
233
+ }).catch(function (error) {
234
+
235
+ // Error
236
+
237
+ console.error('mediaDevice.getUserMedia() error:', error);
238
+
239
+ return;
240
+
241
+ });
242
+
243
+
244
+
245
+ // Peerオブジェクトの作成
246
+
247
+ peer = new Peer({
248
+
249
+ key: '16c0bb93-5ea4-4114-bb5d-af179d53765f',
250
+
251
+ debug: 1
252
+
253
+ });
254
+
255
+
256
+
257
+ //openイベント
258
+
259
+ peer.on('open', function(){
260
+
261
+ $('#my-id').text(peer.id);
262
+
263
+ });
264
+
265
+
266
+
267
+ //errorイベント
268
+
269
+ peer.on('error', function(err){
270
+
271
+ alert(err.message);
272
+
273
+ });
274
+
275
+
276
+
277
+ //closeイベント
278
+
279
+ peer.on('close', function(){
280
+
281
+ });
282
+
283
+
284
+
285
+ //disconnectedイベント
286
+
287
+ peer.on('disconnected', function(){
288
+
289
+ });
290
+
291
+
292
+
293
+ //発信処理
294
+
295
+ $('#make-call').submit(function(e){
296
+
297
+ e.preventDefault();
298
+
299
+ const call = peer.call($('#callto-id').val(), localStream);
300
+
301
+ setupCallEventHandlers(call);
302
+
303
+ $('#countDownWrap').css('display','block');
304
+
305
+ $('#countDown__answer').css('display','none');
306
+
307
+ });
308
+
309
+
310
+
311
+ //切断処理
312
+
313
+ $('#end-call').click(function(){
314
+
315
+ existingCall.close();
316
+
317
+ });
318
+
319
+
320
+
321
+ //着信処理
322
+
323
+ peer.on('call', function(call){
324
+
325
+ call.answer(localStream);
326
+
327
+ setupCallEventHandlers(call);
328
+
329
+ $('#countDownWrap').css('display','block');
330
+
331
+ $('#countDown__call').css('display','none');
332
+
333
+ $('#startBtn').css('display','none');
334
+
335
+ $('#stopBtn').css('display','none');
336
+
337
+ $('#resetBtn').css('display','none');
338
+
339
+ $('#controll').css('display','none');
340
+
341
+ });
342
+
343
+
344
+
345
+ //通話中の接続要求を優先して、既存の接続は切断する(アプリの仕様次第)
346
+
347
+ function setupCallEventHandlers(call){
348
+
349
+ if (existingCall) {
350
+
351
+ existingCall.close();
352
+
353
+ };
354
+
355
+
356
+
357
+ // existingCallを保持
358
+
359
+ existingCall = call;
360
+
361
+
362
+
363
+ // 相手のカメラ映像・マイク音声を受信した際に発火
364
+
365
+ // 取得したStreamオブジェクトをvideo要素にセットする
366
+
367
+ call.on('stream', function(stream){
368
+
369
+ addVideo(call,stream);
370
+
371
+ setupEndCallUI();
372
+
373
+ $('#their-id').text(call.remoteId);
374
+
375
+ //相手方のID名のドキュメント作成
376
+
377
+ var newTimer = db.collection("users").doc(call.remoteId);
378
+
379
+ newTimer.set({
380
+
381
+ time: time
382
+
383
+ })
384
+
385
+ });
386
+
387
+
388
+
389
+ //切断されたら発火
390
+
391
+ call.on('close', function(){
392
+
393
+ removeVideo(call.remoteId);
394
+
395
+ setupMakeCallUI();
396
+
397
+ });
398
+
399
+ }
400
+
401
+
402
+
403
+ //UIのセットアップ
404
+
405
+ function addVideo(call,stream){
406
+
407
+ $('#their-video').get(0).srcObject = stream;
408
+
409
+ }
410
+
411
+
412
+
413
+ function removeVideo(peerId){
414
+
415
+ $('#their-video').get(0).srcObject = undefined;
416
+
417
+ }
418
+
419
+
420
+
421
+ function setupMakeCallUI(){
422
+
423
+ $('#make-call').show();
424
+
425
+ $('#end-call').hide();
426
+
427
+ }
428
+
429
+
430
+
431
+ function setupEndCallUI(){
432
+
433
+ $('#make-call').hide();
434
+
435
+ $('#end-call').show();
436
+
437
+ }
438
+
439
+
440
+
441
+ //firestoreのfunction
442
+
443
+
444
+
445
+
446
+
447
+ function takeTimer() {
448
+
449
+ newTimer.get().then(function(doc) {
450
+
451
+ if(doc.exists){
452
+
453
+ let result = '';
454
+
455
+ let data = doc.data();
456
+
457
+ result = data.time;
458
+
459
+ return result;
460
+
461
+ // $('#countDown').text(result);
462
+
463
+ console.log(result);
464
+
465
+ } else {
466
+
467
+ console.log("No such a document!");
468
+
469
+ }
470
+
471
+ }).catch(function(error) {
472
+
473
+ console.log("Error getting document:", error);
474
+
475
+ })
476
+
477
+ }
478
+
479
+
480
+
481
+ //タイマーの設定-----------------------------------
482
+
483
+
484
+
485
+ var setSecond = 100; //タイマーの秒数
486
+
487
+ var setPause = setSecond;//ストップ時の秒数を保存
488
+
489
+ var time=setSecond; //残り秒数を保存
490
+
491
+ var timerID; //setInterval用の変数
492
+
493
+
494
+
495
+ //残り秒数を表示させる関数
496
+
497
+ function textDisplay(){
498
+
499
+ $('#countDown').text(time);
500
+
501
+ }
502
+
503
+
504
+
505
+ //カウントを1減らす関数
506
+
507
+ function countDown(){
508
+
509
+ time--;
510
+
511
+ // setTimer();
512
+
513
+ // takeTimer();
514
+
515
+ // $('#countDown__answer').text(result);
516
+
517
+ setPause = time; //ストップ時に使用するために残り秒数を代入
518
+
519
+ textDisplay();
520
+
521
+ }
522
+
523
+
524
+
525
+ //タイマーの停止用関数
526
+
527
+ function countStop(){
528
+
529
+ clearInterval(timerID);
530
+
531
+ }
532
+
533
+
534
+
535
+ //タイマースタートの関数
536
+
537
+ function timerStart(){
538
+
539
+ countStop(); //カウントダウンの重複を防ぐため、今動いているタイマーをクリア
540
+
541
+ timerID = setInterval(function(){
542
+
543
+ if(time <= 0) {
544
+
545
+ clearInterval(timerID);
546
+
547
+ existingCall.close();
548
+
549
+ } else {
550
+
551
+ countDown();
552
+
553
+ }
554
+
555
+ }, 1000);
556
+
557
+ }
558
+
559
+
560
+
561
+ // ボタンの実行処理---------------------------------------
562
+
563
+ textDisplay();
564
+
565
+ // setTimer();
566
+
567
+ // takeTimer();
568
+
569
+
570
+
571
+ //タイマースタート
572
+
573
+ $("#startBtn").click(function(){
574
+
575
+ time= setPause;//途中からでも開始出来る
576
+
577
+ textDisplay();
578
+
579
+ timerStart();
580
+
581
+ });
582
+
583
+
584
+
585
+ //タイマー停止
586
+
587
+ $("#stopBtn").click(function(){
588
+
589
+ countStop();
590
+
591
+ });
592
+
593
+
594
+
595
+ //タイマー初期化
596
+
597
+ $("#resetBtn").click(function(){
598
+
599
+ countStop();
600
+
601
+ time = setPause = setSecond; //初期値に初期化
602
+
603
+ textDisplay();
604
+
605
+ });
606
+
607
+
608
+
609
+ //秒数変更機能
610
+
611
+ $("#changeSecond").click(function(){
612
+
613
+
614
+
615
+ var seveSecond = $("#setSecond").val();
616
+
617
+
618
+
619
+ if(seveSecond.match( /[^0-9]+/ )){ //^は否定の文字クラス
620
+
621
+ $("#error").text("※半角数字で入力してください")
622
+
623
+
624
+
625
+ } else if(seveSecond=='') {
626
+
627
+ $("#error").text("※値を入力してください")
628
+
629
+ } else {
630
+
631
+ $("#error").text("")
632
+
633
+ setSecond = seveSecond;
634
+
635
+ countStop();
636
+
637
+ time = setPause = setSecond;
638
+
639
+ textDisplay();
640
+
641
+ }
642
+
643
+ });
644
+
645
+
646
+
647
+
648
+
649
+ var h = document.getElementById('their-id');
650
+
651
+ console.log(h.innerHTML);//タグ内のテキストを取り出そうとしています。
652
+
653
+ ```