質問編集履歴
1
文章や質問の明確化
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
WebRTCのテキストデータ送信
|
1
|
+
WebRTCのテキストデータ送信を変数として使う方法
|
test
CHANGED
@@ -2,11 +2,9 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
WebRTCを用いた動画ストリーミング兼コントロールのアプリケーションを作成しております。
|
5
|
+
WebRTCを用いた動画ストリーミング兼コントロールのwebアプリケーションを作成しております。
|
6
|
-
|
6
|
+
|
7
|
-
アプリケーションの開発に当たってはSkywayのJavascriptSDK
|
7
|
+
アプリケーションの開発に当たってはSkywayのJavascriptSDKチュートリアルのコードを使用させていただき、ルーム通話を想定しています。
|
8
|
-
|
9
|
-
ここで、Peerで表示させたボタンをクリックしたときのイベントとして、もう一つのPeer内部で動いているC++のプログラムでイベントを動作させステータス変数を書き換えたいというのが目標です。
|
10
8
|
|
11
9
|
|
12
10
|
|
@@ -14,9 +12,11 @@
|
|
14
12
|
|
15
13
|
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
ここで、0や1というデータを送信し
|
15
|
+
Skywayにて実装されているテキストチャットを行うためのSDKによりテキストデータの1や0を送信し、テキストを受け取ったもう一つのPeerは、1か0の受けとったデータによってifやswitch文等でc++のプログラムを動作させるということを行いたいです。
|
16
|
+
|
17
|
+
ここで、javascript後ろの方にあるonClicksend関数により,0や1というデータを送信したものの一向にif文のtrueが判定されずに困っております。peer間を経由する際に、メッセージがどういった変数に格納されているのか把握ができておりません。localTextはテキストボックス、messagesはhtmlへ出力するメッセージ及びpeerIDだと考えられるのでどういった送信アルゴリズムであるか全体像をつかめておりません。これらについて、ご教授いただければと存じます。
|
18
|
+
|
19
|
+
また、skywayのテキストデータ送信にはUDPプロトコルが使用されているとの記述があったのですが、チュートリアルコードにはWebsocketという記述がみられます。websocketはTCPで動作するものかと思いますがこれはどういった理由なのかも理解が不十分であるため困っております。どうぞよろしくお願いいたします。
|
20
20
|
|
21
21
|
|
22
22
|
|
@@ -24,10 +24,6 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
index.html
|
28
|
-
|
29
|
-
|
30
|
-
|
31
27
|
<!DOCTYPE html>
|
32
28
|
|
33
29
|
<html lang="en">
|
@@ -38,68 +34,68 @@
|
|
38
34
|
|
39
35
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
40
36
|
|
41
|
-
<title>
|
37
|
+
<title>P2PCommunication!!</title>
|
42
38
|
|
43
39
|
<link rel="stylesheet" href="../_shared/style.css">
|
44
40
|
|
45
41
|
</head>
|
46
42
|
|
47
|
-
|
48
|
-
|
49
43
|
<body>
|
50
44
|
|
51
45
|
<div class="container">
|
52
46
|
|
53
|
-
<h1 class="heading">
|
47
|
+
<h1 class="heading">Offer</h1>
|
54
48
|
|
55
49
|
<p class="note">
|
56
50
|
|
57
|
-
|
51
|
+
Change Room mode (before join in a room):
|
52
|
+
|
53
|
+
<a href="#">mesh</a> / <a href="#sfu">sfu</a>
|
58
54
|
|
59
55
|
</p>
|
60
56
|
|
61
|
-
|
62
|
-
|
63
|
-
<div class="
|
57
|
+
<div class="room">
|
64
|
-
|
58
|
+
|
65
|
-
<div
|
59
|
+
<div>
|
66
|
-
|
60
|
+
|
67
|
-
<video id="js-
|
61
|
+
<video id="js-local-stream"></video>
|
62
|
+
|
63
|
+
<br>
|
64
|
+
|
65
|
+
<p>Room mode : <span id="js-room-mode"></span></p>
|
66
|
+
|
67
|
+
<p>Your ID: <span id="js-local-Id"></span></p>
|
68
|
+
|
69
|
+
<input type="text" placeholder="Room Name" id="js-room-id">
|
70
|
+
|
71
|
+
<button id="js-join-trigger">Join</button>
|
72
|
+
|
73
|
+
<button id="js-leave-trigger">Leave</button>
|
68
74
|
|
69
75
|
</div>
|
70
76
|
|
71
|
-
|
77
|
+
|
72
|
-
|
78
|
+
|
73
|
-
|
79
|
+
<div class="remote-streams" id="js-remote-streams"></div>
|
80
|
+
|
81
|
+
|
82
|
+
|
74
|
-
|
83
|
+
<div>
|
84
|
+
|
75
|
-
<p
|
85
|
+
<pre class="messages" id="js-messages"></pre>
|
76
|
-
|
86
|
+
|
77
|
-
<input type="text"
|
87
|
+
<input type="text" id="js-local-text">
|
78
|
-
|
88
|
+
|
79
|
-
<button id="js-
|
89
|
+
<button id="js-send-trigger">Send</button>
|
80
|
-
|
81
|
-
|
90
|
+
|
82
|
-
|
83
|
-
<br>
|
84
|
-
|
85
|
-
<button id="js-start-trigger">Start</button>
|
91
|
+
<button id="js-start-trigger">Start</button>
|
86
|
-
|
92
|
+
|
87
|
-
<button id="js-stop-trigger">Stop</button>
|
93
|
+
<button id="js-stop-trigger">Stop</button>
|
88
94
|
|
89
95
|
</div>
|
90
96
|
|
91
97
|
</div>
|
92
98
|
|
93
|
-
<div>
|
94
|
-
|
95
|
-
<pre class="messages" id="js-messages"></pre>
|
96
|
-
|
97
|
-
<input type="text" id="js-local-text">
|
98
|
-
|
99
|
-
<button id="js-send-trigger">Send</button>
|
100
|
-
|
101
|
-
</div>
|
102
|
-
|
103
99
|
<p class="meta" id="js-meta"></p>
|
104
100
|
|
105
101
|
</div>
|
@@ -116,7 +112,7 @@
|
|
116
112
|
|
117
113
|
|
118
114
|
|
119
|
-
s
|
115
|
+
const Peer = window.Peer;
|
120
116
|
|
121
117
|
|
122
118
|
|
@@ -124,23 +120,29 @@
|
|
124
120
|
|
125
121
|
const localVideo = document.getElementById('js-local-stream');
|
126
122
|
|
127
|
-
const localId = document.getElementById('js-local-id');
|
128
|
-
|
129
|
-
const
|
123
|
+
const joinTrigger = document.getElementById('js-join-trigger');
|
130
|
-
|
124
|
+
|
131
|
-
const
|
125
|
+
const leaveTrigger = document.getElementById('js-leave-trigger');
|
132
|
-
|
126
|
+
|
133
|
-
const remoteVideo = document.getElementById('js-remote-stream');
|
127
|
+
const remoteVideos = document.getElementById('js-remote-streams');
|
134
|
-
|
128
|
+
|
135
|
-
const r
|
129
|
+
const roomId = document.getElementById('js-room-id');
|
130
|
+
|
131
|
+
const roomMode = document.getElementById('js-room-mode');
|
132
|
+
|
133
|
+
const localText = document.getElementById('js-local-text');
|
134
|
+
|
135
|
+
const localId= document.getElementById('js-local-Id');
|
136
|
+
|
137
|
+
const sendTrigger = document.getElementById('js-send-trigger');
|
138
|
+
|
139
|
+
const messages = document.getElementById('js-messages');
|
136
140
|
|
137
141
|
const meta = document.getElementById('js-meta');
|
138
142
|
|
139
143
|
const sdkSrc = document.querySelector('script[src*=skyway]');
|
140
144
|
|
141
|
-
const sta
|
145
|
+
const status = document.getElementById('js-start-trigger');
|
142
|
-
|
143
|
-
const stopTrigger = document.getElementById('js-stop-trigger');
|
144
146
|
|
145
147
|
|
146
148
|
|
@@ -154,6 +156,22 @@
|
|
154
156
|
|
155
157
|
|
156
158
|
|
159
|
+
const getRoomModeByHash = () => (location.hash === '#sfu' ? 'sfu' : 'mesh');
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
roomMode.textContent = getRoomModeByHash();
|
164
|
+
|
165
|
+
window.addEventListener(
|
166
|
+
|
167
|
+
'hashchange',
|
168
|
+
|
169
|
+
() => (roomMode.textContent = getRoomModeByHash())
|
170
|
+
|
171
|
+
);
|
172
|
+
|
173
|
+
|
174
|
+
|
157
175
|
const localStream = await navigator.mediaDevices
|
158
176
|
|
159
177
|
.getUserMedia({
|
@@ -180,21 +198,21 @@
|
|
180
198
|
|
181
199
|
|
182
200
|
|
201
|
+
// eslint-disable-next-line require-atomic-updates
|
202
|
+
|
183
|
-
|
203
|
+
const peer = (window.peer = new Peer({
|
184
|
-
|
204
|
+
|
185
|
-
|
205
|
+
key:'e316eaa7-4c1c-468c-b23a-9ce51b074ab7' ,
|
186
|
-
|
206
|
+
|
187
|
-
|
207
|
+
debug: 3,
|
188
|
-
|
208
|
+
|
189
|
-
|
209
|
+
}));
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
210
|
+
|
211
|
+
|
212
|
+
|
195
|
-
// Register
|
213
|
+
// Register join handler
|
196
|
-
|
214
|
+
|
197
|
-
|
215
|
+
joinTrigger.addEventListener('click', () => {
|
198
216
|
|
199
217
|
// Note that you need to ensure the peer has connected to signaling server
|
200
218
|
|
@@ -208,82 +226,164 @@
|
|
208
226
|
|
209
227
|
|
210
228
|
|
211
|
-
const
|
229
|
+
const room = peer.joinRoom(roomId.value, {
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
230
|
+
|
216
|
-
|
217
|
-
// Render remote stream for caller
|
218
|
-
|
219
|
-
|
231
|
+
mode: getRoomModeByHash(),
|
220
|
-
|
232
|
+
|
221
|
-
remo
|
233
|
+
stream: localStream,
|
222
|
-
|
223
|
-
|
234
|
+
|
224
|
-
|
225
|
-
});
|
235
|
+
});
|
226
|
-
|
227
|
-
|
228
|
-
|
236
|
+
|
237
|
+
|
238
|
+
|
229
|
-
|
239
|
+
room.once('open', () => {
|
240
|
+
|
241
|
+
messages.textContent += '=== You joined ===\n';
|
242
|
+
|
243
|
+
});
|
244
|
+
|
245
|
+
room.on('peerJoin', peerId => {
|
246
|
+
|
247
|
+
messages.textContent += `=== ${peerId} joined ===\n`;
|
248
|
+
|
249
|
+
});
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
// Render remote stream for new peer join in the room
|
254
|
+
|
255
|
+
room.on('stream', async stream => {
|
256
|
+
|
257
|
+
const newVideo = document.createElement('video');
|
258
|
+
|
259
|
+
newVideo.srcObject = stream;
|
260
|
+
|
261
|
+
newVideo.playsInline = true;
|
262
|
+
|
263
|
+
// mark peerId to find it later at peerLeave event
|
264
|
+
|
265
|
+
newVideo.setAttribute('data-peer-id', stream.peerId);
|
266
|
+
|
267
|
+
remoteVideos.append(newVideo);
|
268
|
+
|
269
|
+
await newVideo.play().catch(console.error);
|
270
|
+
|
271
|
+
});
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
room.on('data', ({ data, src }) => {
|
276
|
+
|
277
|
+
// Show a message sent to the room and who sent
|
278
|
+
|
279
|
+
messages.textContent += `${src}: ${data}\n`;
|
280
|
+
|
281
|
+
});
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
// for closing room members
|
286
|
+
|
287
|
+
room.on('peerLeave', peerId => {
|
288
|
+
|
289
|
+
const remoteVideo = remoteVideos.querySelector(
|
290
|
+
|
291
|
+
`[data-peer-id=${peerId}]`
|
292
|
+
|
293
|
+
);
|
230
294
|
|
231
295
|
remoteVideo.srcObject.getTracks().forEach(track => track.stop());
|
232
296
|
|
233
297
|
remoteVideo.srcObject = null;
|
234
298
|
|
299
|
+
remoteVideo.remove();
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
messages.textContent += `=== ${peerId} left ===\n`;
|
304
|
+
|
235
|
-
});
|
305
|
+
});
|
306
|
+
|
307
|
+
|
308
|
+
|
236
|
-
|
309
|
+
// for closing myself
|
310
|
+
|
237
|
-
|
311
|
+
room.once('close', () => {
|
312
|
+
|
238
|
-
|
313
|
+
sendTrigger.removeEventListener('click', onClickSend);
|
314
|
+
|
315
|
+
messages.textContent += '== You left ===\n';
|
316
|
+
|
317
|
+
Array.from(remoteVideos.children).forEach(remoteVideo => {
|
318
|
+
|
319
|
+
remoteVideo.srcObject.getTracks().forEach(track => track.stop());
|
320
|
+
|
321
|
+
remoteVideo.srcObject = null;
|
322
|
+
|
323
|
+
remoteVideo.remove();
|
324
|
+
|
325
|
+
});
|
326
|
+
|
327
|
+
});
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
sendTrigger.addEventListener('click', onClickSend);
|
332
|
+
|
333
|
+
sendTrigger.addEventListener('click', onClickStart);
|
334
|
+
|
335
|
+
sendTrigger.addEventListener('click', onClickStop);
|
336
|
+
|
239
|
-
|
337
|
+
leaveTrigger.addEventListener('click', () => room.close(), { once: true });
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
function onClickSend() {
|
342
|
+
|
343
|
+
// Send message to all of the peers in the room via websocket
|
344
|
+
|
345
|
+
room.send(localText.value);
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
messages.textContent += `${peer.id}: ${localText.value}\n`;
|
350
|
+
|
351
|
+
localText.value = '';
|
352
|
+
|
353
|
+
if(localText === 1) {
|
354
|
+
|
355
|
+
window.alert('走行を開始します。大丈夫ですか?');
|
356
|
+
|
357
|
+
} else {
|
358
|
+
|
359
|
+
window.alert('trueが読まれていません');
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
/*function onClickStart() {
|
368
|
+
|
369
|
+
// Send message to all of the peers in the room via websocket
|
370
|
+
|
371
|
+
room.send(localText.value);
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
startx.textContent += `${peer.id}: ${1}\n`;
|
376
|
+
|
377
|
+
startx.value = ' ';
|
378
|
+
|
379
|
+
} */
|
380
|
+
|
381
|
+
|
240
382
|
|
241
383
|
});
|
242
384
|
|
243
385
|
|
244
386
|
|
245
|
-
peer.once('open', id => (localId.textContent = id));
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
// Register callee handler
|
250
|
-
|
251
|
-
peer.on('call', mediaConnection => {
|
252
|
-
|
253
|
-
mediaConnection.answer(localStream);
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
mediaConnection.on('stream', async stream => {
|
258
|
-
|
259
|
-
// Render remote stream for callee
|
260
|
-
|
261
|
-
remoteVideo.srcObject = stream;
|
262
|
-
|
263
|
-
remoteVideo.playsInline = true;
|
264
|
-
|
265
|
-
await remoteVideo.play().catch(console.error);
|
266
|
-
|
267
|
-
});
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
mediaConnection.once('close', () => {
|
272
|
-
|
273
|
-
remoteVideo.srcObject.getTracks().forEach(track => track.stop());
|
274
|
-
|
275
|
-
remoteVideo.srcObject = null;
|
276
|
-
|
277
|
-
});
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
closeTrigger.addEventListener('click', () => mediaConnection.close(true));
|
282
|
-
|
283
|
-
});
|
284
|
-
|
285
|
-
|
286
|
-
|
287
387
|
peer.on('error', console.error);
|
288
388
|
|
289
389
|
})();
|
@@ -292,10 +392,16 @@
|
|
292
392
|
|
293
393
|
|
294
394
|
|
395
|
+
|
396
|
+
|
295
397
|
### 試したこと
|
296
398
|
|
297
399
|
|
298
400
|
|
299
401
|
|
300
402
|
|
403
|
+
|
404
|
+
|
405
|
+
|
406
|
+
|
301
407
|
### 補足情報(FW/ツールのバージョンなど)
|