質問するログイン新規登録

質問編集履歴

2

APIキーの格納方法

2020/07/17 14:34

投稿

mamiduka
mamiduka

スコア11

title CHANGED
File without changes
body CHANGED
@@ -104,4 +104,18 @@
104
104
  これがビデオ通話するときに必要なAPIキーを記述しているところです。
105
105
  ローカル環境では.envファイルにAPIキーを記述しており、ローカル環境ではきちんと動作しました。
106
106
 
107
- デプロイはherokuで行っております。
107
+ デプロイはherokuで行っております。
108
+
109
+ APIキーの格納方法
110
+ ```
111
+ gem 'dotenv-rails'
112
+ gem 'gon'
113
+ ```
114
+
115
+ ```
116
+ def show
117
+ gon.api_key = ENV['API_KEY']
118
+ ```
119
+ ```env
120
+ API_KEY =xxxxxxxxxxxxxxxxxx
121
+ ```

1

JavaScriptの内容追記

2020/07/17 14:34

投稿

mamiduka
mamiduka

スコア11

title CHANGED
File without changes
body CHANGED
@@ -18,4 +18,90 @@
18
18
 
19
19
  おそらくAPIキーの格納場所が違うと思うのですがどこにどのように格納すれば良いのかわかりません。
20
20
 
21
- よろしくお願いします。
21
+ よろしくお願いします。
22
+
23
+
24
+ # 追記
25
+ 用語が分からず的外れな追記であればおっしゃってください、申し訳ございません。
26
+
27
+ [作成したアプリです](https://myquestionapp.herokuapp.com/)
28
+ ↑適当に新規登録をしていただき、[こちら](https://myquestionapp.herokuapp.com/users/1)に入っていただきますとサイトの下部にビデオ通話が開始するボタンがあります。
29
+ コンソールでエラーが出ていますので確認をしていただきたいです。申し訳ありません。
30
+
31
+ ```javascript
32
+ document.getElementById("call-media-contents").style.display ="none";
33
+ document.getElementById("before-call").addEventListener("click", function() {
34
+ alert("カメラがオンになります");
35
+ alert("IDが発行されます。相手に教えることでビデオ通話ができます");
36
+ const contents = document.getElementById("call-media-contents");
37
+ const button = document.getElementById("before-call-contents");
38
+ if(contents.style.display=="block"){
39
+ contents.style.display ="none";
40
+ button.style.display ="block";
41
+ }else{
42
+ contents.style.display ="block";
43
+ button.style.display ="none";
44
+ }
45
+
46
+ let localStream;
47
+
48
+ navigator.mediaDevices.getUserMedia({video: true, audio: true})
49
+ .then( stream => {
50
+ const videoElm = document.getElementById('my-video')
51
+ videoElm.srcObject = stream;
52
+ videoElm.play();
53
+ localStream = stream;
54
+ }).catch( error => {
55
+ console.error('mediaDevice.getUserMedia() error:', error);
56
+ return;
57
+ });
58
+
59
+ const peer = new Peer({
60
+ key: gon.api_key,
61
+ debug: 3
62
+ });
63
+ peer.on('open', () => {
64
+ document.getElementById('my-id').textContent = peer.id;
65
+ });
66
+ peer.on('error', err => {
67
+ alert(err.message);
68
+ });
69
+ peer.on('close', () => {
70
+ alert('通信が切断しました。');
71
+ });
72
+
73
+ document.getElementById('make-call').onclick = () => {
74
+ const theirID = document.getElementById('their-id').value;
75
+ const mediaConnection = peer.call(theirID, localStream);
76
+ setEventListener(mediaConnection);
77
+ };
78
+
79
+ document.getElementById('stop-button').addEventListener("click", function() {
80
+ location.reload();
81
+ });
82
+
83
+ const setEventListener = mediaConnection => {
84
+ mediaConnection.on('stream', stream => {
85
+ const videoElm = document.getElementById('their-video')
86
+ videoElm.srcObject = stream;
87
+ videoElm.play();
88
+ });
89
+ }
90
+ peer.on('call', mediaConnection => {
91
+ mediaConnection.answer(localStream);
92
+ setEventListener(mediaConnection);
93
+ });
94
+ });
95
+ ```
96
+
97
+ 上記のコードの真ん中あたりの
98
+ ```
99
+ const peer = new Peer({
100
+ key: gon.api_key,
101
+ debug: 3
102
+ });
103
+ ```
104
+ これがビデオ通話するときに必要なAPIキーを記述しているところです。
105
+ ローカル環境では.envファイルにAPIキーを記述しており、ローカル環境ではきちんと動作しました。
106
+
107
+ デプロイはherokuで行っております。