forked from mozilla/webrtc-landing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_test_old.html
270 lines (237 loc) · 7.5 KB
/
data_test_old.html
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
<html>
<head>
<title>Simple WebRTC Data Channel Test</title>
</head>
<body>
<table width=100% height=100%>
<tr><td><h1>Simple WebRTC Data Channel Test</h1></td></tr>
<tr><td>(Note: this JS code is REALLY UGLY)</td></tr>
<tr><td><div><button id="thebutton" onClick="start();">Start!</button></div><br/></td></tr>
<tr><td><form id="pc1_form" action="javascript:sendit(1)">
<div>pc1 says: <input id="pc1_input" type="text" value="type here" onKeyPress="return submitenter(this,event)"/>
<input type="submit"/></div></form></td>
<td><form id="pc1_blob" action="javascript:sendblob(1)">
<div>pc1 sends a blob: <input id="pc1_browse" type="file"/>
<input type="submit"/></div></form></td></tr>
<tr><td><form id="pc2_form" action="javascript:sendit(2)">
<div>pc2 says: <input id="pc2_input" type="text" value="type here" onKeyPress="return submitenter(this,event)"/>
<input type="submit"/></div></form></td>
<td><form id="pc2_blob" action="javascript:sendblob(2)">
<div>pc2 sends a blob: <input id="pc2_browse" type="file"/>
<input type="submit"/></div></form></td></tr>
<tr><td><div id="datawindow" style="
width: 100%;
height: 500px;
overflow: auto;
border: 1px solid red;"></div></td></tr>
<script type="application/javascript">
let button = document.getElementById("thebutton");
let text_pc1 = document.getElementById("pc1_input");
let text_pc2 = document.getElementById("pc2_input");
let blob_pc1 = document.getElementById("pc1_browse");
let blob_pc2 = document.getElementById("pc2_browse");
let datawindow = document.getElementById("datawindow");
let pc1;
let pc2;
let dc1;
let dc2;
let channel1;
let channel2;
let num_channels;
num_channels = 0;
var datachannels = new Array(0);
let pc1_offer;
let pc2_answer;
let iter = 0;
let iter2 = 0;
function log(msg) {
let div = document.getElementById("datawindow");
div.innerHTML = div.innerHTML + "<p>" + msg + "</p>";
}
var fancy_log = function(msg,color) {
var pre = document.createElement("p");
var message = '<span style="color: ' + color + ';">' + msg + '</span>';
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
datawindow.appendChild(pre); // (window).* here doesn't work right
pre.scrollIntoView(false);
};
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13) {
myfield.form.submit();
return false;
} else {
return true;
}
}
var sendit = function (which) {
iter = iter + 1;
//log("Sending message #" + iter + " this = " + this);
if (which == 1) {
dc1.send(text_pc1.value);
text_pc1.value = "";
} else if (which == 2) {
dc2.send(text_pc2.value);
text_pc2.value = "";
} else {
log("Unknown send " + which);
}
};
var sendblob = function (which) {
iter = iter + 1;
//log("Sending blob #" + iter + " this = " + this);
if (which == 1) {
dc1.send(blob_pc1.files[0]);
blob_pc1.value = "";
} else if (which == 2) {
dc2.send(blob_pc2.files[0]);
blob_pc2.value = "";
} else {
log("Unknown sendblob " + which);
}
};
function failed(code) {
log("Failure callback: " + code);
}
// pc1.createOffer finished, call pc1.setLocal
function step1(offer) {
pc1_offer = offer;
log("Offer: " + pc1_offer.sdp);
pc1.setLocalDescription(offer, step1_5, failed);
}
function step1_5() {
setTimeout(step2,0);
}
// pc1.setLocal finished, call pc2.setRemote
function step2() {
pc2 = new RTCPeerConnection();
pc2.ondatachannel = function(channel) {
log("pc2 onDataChannel [" +num_channels + "] = " + channel +
", label='" + channel.label + "'");
dc2 = channel;
datachannels[num_channels] = channel;
num_channels++;
channel.binaryType = "blob";
channel.onmessage = function(evt) {
iter2 = iter2 + 1;
if (evt.data instanceof Blob) {
fancy_log("*** pc1 sent Blob: " + evt.data + ", length=" + evt.data.size,"red");
} else {
fancy_log("pc1 said: " + evt.data,"red");
}
};
channel.onopen = function() {
log("*** pc2 onopen fired, sending to " + channel);
channel.send("pc2 says Hi there!");
};
channel.onclose = function() {
log("*** pc2 onclose fired");
};
};
pc2.onconnection = function() {
log("pc2 onConnection ");
}
pc2.onclosedconnection = function() {
log("pc2 onClosedConnection ");
}
pc2.onaddstream = function(obj) {
log("pc2 got remote stream from pc1 " + obj.type);
}
pc2.setRemoteDescription(pc1_offer, step3, failed);
};
// pc2.setRemote finished, call pc2.createAnswer
function step3() {
pc2.createAnswer(step4, failed);
}
// pc2.createAnswer finished, call pc2.setLocal
function step4(answer) {
pc2_answer = answer;
pc2.setLocalDescription(answer, step5, failed);
}
// pc2.setLocal finished, call pc1.setRemote
function step5() {
log("Answer: " + pc2_answer.sdp);
pc1.setRemoteDescription(pc2_answer, step6, failed);
}
// pc1.setRemote finished, make a data channel
function step6() {
log("HIP HIP HOORAY");
// setTimeout(step7,0);
}
function step7() {
pc1.connectDataConnection(5000,5001);
pc2.connectDataConnection(5001,5000);
log("connect for data channel called");
}
function start() {
button.innerHTML = "Stop!";
button.onclick = stop;
while (datawindow.firstChild) {
datawindow.removeChild(datawindow.firstChild);
}
pc1 = new RTCPeerConnection();
pc1.onaddstream = function(obj) {
log("pc1 got remote stream from pc2 " + obj.type);
}
pc1.ondatachannel = function(channel) {
// In case pc2 opens a channel
log("pc1 onDataChannel [" +num_channels + "] = " + channel +
", label='" + channel.label + "'");
datachannels[num_channels] = channel;
num_channels++;
channel.onmessage = function(evt) {
if (evt.data instanceof Blob) {
fancy_log("*** pc2 sent Blob: " + evt.data + ", length=" + evt.data.size,"blue");
} else {
fancy_log('pc2 said: ' + evt.data,"blue");
}
}
channel.onopen = function() {
log("pc1 onopen fired for " + channel);
channel.send("pc1 says Hello out there...");
log("pc1 state: " + channel.state);
}
channel.onclose = function() {
log("pc1 onclose fired");
};
}
pc1.onconnection = function() {
log("pc1 onConnection ");
dc1 = pc1.createDataChannel("This is pc1",{}); // reliable (TCP-like)
// dc1 = pc1.createDataChannel("This is pc1",{outOfOrderAllowed: true, maxRetransmitNum: 0}); // unreliable (UDP-like)
channel = dc1;
channel.binaryType = "blob";
channel.onmessage = function(evt) {
if (evt.data instanceof Blob) {
fancy_log("*** pc2 sent Blob: " + evt.data,"blue");
} else {
fancy_log('pc2 said: ' + evt.data,"blue");
}
}
channel.onopen = function() {
log("pc1 onopen fired for " + channel);
channel.send("pc1 says Hello...");
}
channel.onclose = function() {
log("pc1 onclose fired");
};
}
pc1.onclosedconnection = function() {
log("pc1 onClosedConnection ");
}
pc1.createOffer(step1, failed);
}
function stop() {
log("closed");
pc1.close();
pc2.close();
button.innerHTML = "Start!";
button.onclick = start;
}
</script>
</html>