-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
index.html
355 lines (288 loc) · 14.3 KB
/
index.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
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
<!--
> Muaz Khan - https://github.com/muaz-khan
> MIT License - https://www.webrtc-experiment.com/licence/
> Documentation - https://github.com/muaz-khan/WebRTC-Experiment/tree/master/DataChannel
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>DataChannel.js » A WebRTC Library for Data Sharing</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel="author" type="text/html" href="https://plus.google.com/+MuazKhan">
<meta name="author" content="Muaz Khan">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="https://cdn.webrtc-experiment.com/style.css">
<style>
#chat-output div, #file-progress div {
border: 1px solid black;
border-bottom: 0;
padding: .1em .4em;
}
input {
border: 1px solid black;
font-family: inherit;
font-size: 1em;
margin: .1em .3em;
outline: none;
padding: .1em .2em;
width: 97%;
}
#chat-output, #file-progress {
margin: 0 0 0 .4em;
max-height: 12em;
overflow: auto;
}
</style>
<script>
document.createElement('article');
document.createElement('footer');
</script>
<!-- DataChannel.js library-->
<script src="https://cdn.webrtc-experiment.com/DataChannel.js"> </script>
<script src="https://cdn.webrtc-experiment.com/socket.io.js"> </script>
</head>
<body>
<article>
<header style="text-align: center;">
<h1>
<a href="https://github.com/muaz-khan/DataChannel" target="_blank">DataChannel.js</a>
» A WebRTC Library for Data Sharing
</h1>
<p>
<a href="https://www.webrtc-experiment.com/">HOME</a>
<span> © </span>
<a href="http://www.MuazKhan.com/" target="_blank">Muaz Khan</a>
.
<a href="http://twitter.com/WebRTCWeb" target="_blank" title="Twitter profile for WebRTC Experiments">@WebRTCWeb</a>
.
<a href="https://github.com/muaz-khan?tab=repositories" target="_blank" title="Github Profile">Github</a>
.
<a href="https://github.com/muaz-khan/DataChannel/issues?state=open" target="_blank">Latest issues</a>
.
<a href="https://github.com/muaz-khan/DataChannel/commits/master" target="_blank">What's New?</a>
</p>
</header>
<div class="github-stargazers"></div>
<!-- just copy this <section> and next script -->
<section class="experiment">
<section>
<h2 style="border: 0; padding-left: .5em;">Open New DataChannel Connection</h2>
<button id="open-channel">Open</button>
</section>
<table style="width: 100%;" id="channels-list"></table>
<table style="width: 100%;">
<tr>
<td>
<h2 style="display: block; font-size: 1em; text-align: center;">Text Chat</h2>
<div id="chat-output"></div>
<input type="text" id="chat-input" style="font-size: 1.2em;" placeholder="chat message" disabled>
</td>
<td style="background: white;">
<h2 style="display: block; font-size: 1em; text-align: center;">Share Files</h2>
<input type="file" id="file" disabled>
<div id="file-progress"></div>
</td>
</tr>
</table>
</section>
<script>
var channel = new DataChannel(); // DataChannel.js
// https://github.com/muaz-khan/WebRTC-Experiment/tree/master/socketio-over-nodejs
var SIGNALING_SERVER = 'https://socketio-over-nodejs2.herokuapp.com:443/';
channel.openSignalingChannel = function(config) {
var channel = config.channel || this.channel || 'default-namespace';
var sender = Math.round(Math.random() * 9999999999) + 9999999999;
io.connect(SIGNALING_SERVER).emit('new-channel', {
channel: channel,
sender : sender
});
var socket = io.connect(SIGNALING_SERVER + channel);
socket.channel = channel;
socket.on('connect', function () {
if (config.callback) config.callback(socket);
});
socket.send = function (message) {
socket.emit('message', {
sender: sender,
data : message
});
};
socket.on('message', config.onmessage);
};
// "ondatachannel" is fired for each new data channel
channel.ondatachannel = function(channel00) {
var alreadyExist = document.getElementById(channel00.id);
if (alreadyExist) return;
var tr = document.createElement('tr');
tr.setAttribute('id', channel00.owner);
tr.innerHTML = '<td>' + channel00.id + '</td>' +
'<td><button class="join" id="' + channel00.id + '">Join</button></td>';
channelsList.insertBefore(tr, channelsList.firstChild);
// when someone clicks table-row; joining the relevant data channel
tr.onclick = function() {
// manually joining a data channel
channel.join({
id: this.querySelector('.join').id,
owner: this.id
});
channelsList.style.display = 'none';
};
};
// a text message or data
channel.onmessage = function(data, userid, latency) {
appendDIV(data);
console.debug(userid, 'posted', data);
console.log('latency:', latency, 'ms');
};
// on data connection gets open
channel.onopen = function() {
if (document.getElementById('chat-input')) document.getElementById('chat-input').disabled = false;
if (document.getElementById('file')) document.getElementById('file').disabled = false;
if (document.getElementById('open-channel')) document.getElementById('open-channel').disabled = true;
};
// sending/receiving file(s)
// channel.autoSaveToDisk = false;
channel.onFileProgress = function(packets, uuid) {
appendDIV(uuid + ': ' + packets.remaining + '..', 'file', fileProgress);
};
channel.onFileSent = function(file) {
appendDIV(file.name + ' sent.', fileProgress);
};
channel.onFileReceived = function(fileName) {
appendDIV(fileName + ' received.', fileProgress);
};
document.getElementById('file').onchange = function() {
channel.send(this.files[0]);
};
var chatOutput = document.getElementById('chat-output'),
fileProgress = document.getElementById('file-progress');
function appendDIV(data, parent) {
var div = document.createElement('div');
div.innerHTML = data;
if (!parent) chatOutput.insertBefore(div, chatOutput.firstChild);
else fileProgress.insertBefore(div, fileProgress.firstChild);
div.tabIndex = 0;
div.focus();
chatInput.focus();
}
var chatInput = document.getElementById('chat-input');
chatInput.onkeypress = function(e) {
if (e.keyCode !== 13 || !this.value) return;
appendDIV(this.value);
// sending text message
channel.send(this.value);
this.value = '';
this.focus();
};
// users presence detection
channel.onleave = function(userid) {
var message = 'A user whose id is ' + userid + ' left you!';
appendDIV(message);
console.warn(message);
};
var channelsList = document.getElementById('channels-list');
document.getElementById('open-channel').onclick = function() {
this.disabled = true;
channel.open();
};
// searching for existing channels
channel.connect();
</script>
<section class="experiment">
<h2 class="header">DataChannel.js Features:</h2>
<ol>
<li>Direct messages — to any user using his `user-id`</li>
<li>Eject/Reject any user — using his `user-id`</li>
<li>Leave any room (i.e. data session) or close entire session using `leave` method</li>
<li>File size is limitless!</li>
<li>Text message length is limitless!</li>
<li>Size of data is also limitless!</li>
<li>Fallback to socket.io/websockets/etc.</li>
<li>Users' presence detection using `onleave`</li>
<li>Latency detection</li>
<li>Multi-longest strings/files concurrent transmission</li>
</ol>
</section>
<section class="experiment">
<h2 class="header">How to use DataChannel.js?</h2>
<pre>
<script src="https://cdn.webrtc-experiment.com/DataChannel.js"> </script>
<input type="text" id="chat-input" disabled
style="font-size: 2em; width: 98%;"><br />
<div id="chat-output"></div>
<script>
var chatOutput = document.getElementById('chat-output');
var chatInput = document.getElementById('chat-input');
chatInput.onkeypress = function(e) {
if (e.keyCode != 13) return;
channel.send(this.value);
chatOutput.innerHTML = 'Me: ' + this.value + '<hr />'
+ chatOutput.innerHTML;
this.value = '';
};
</script>
<script>
var channel = new DataChannel('Session Unique Identifier');
channel.onopen = function(userid) {
chatInput.disabled = false;
chatInput.value = 'Hi, ' + userid;
chatInput.focus();
};
channel.onmessage = function(message, userid) {
chatOutput.innerHTML = userid + ': ' + message + '<hr />'
+ chatOutput.innerHTML;
};
channel.onleave = function(userid) {
chatOutput.innerHTML = userid + ' Left.<hr />'
+ chatOutput.innerHTML;
};
</script>
</pre>
</section>
<section class="experiment">
<h2 class="header">Use your own socket.io for signaling</h2>
<pre>
<script>
// by default socket.io is used for signaling; you can override it
channel.<strong>openSignalingChannel</strong> = function(config) {
var socket = io.connect('http://your-site:8888');
socket.channel = config.channel || this.channel || 'default-channel';
socket.on('message', config.onmessage);
socket.send = function (data) {
socket.emit('message', data);
};
if (config.onopen) setTimeout(config.onopen, 1);
return socket;
}
</script>
</pre>
</section>
<section class="experiment own-widgets latest-commits">
<h2 class="header" id="updates" style="color: red; padding-bottom: .1em;"><a href="https://github.com/muaz-khan/DataChannel/commits/master" target="_blank">Latest Updates</a></h2>
<div id="github-commits"></div>
</section>
<section class="experiment">
<h2 class="header" id="feedback">Feedback</h2>
<div>
<textarea id="message" style="border: 1px solid rgb(189, 189, 189); height: 8em; margin: .2em; outline: none; resize: vertical; width: 98%;" placeholder="Have any message? Suggestions or something went wrong?"></textarea>
</div>
<button id="send-message" style="font-size: 1em;">Send Message</button><small style="margin-left: 1em;">Enter your email too; if you want "direct" reply!</small>
</section>
</article>
<a href="https://github.com/muaz-khan/DataChannel" class="fork-left"></a>
<footer>
<p>
<a href="https://www.webrtc-experiment.com/">WebRTC Experiments</a>
© <a href="https://plus.google.com/+MuazKhan" rel="author" target="_blank">Muaz Khan</a>
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
</p>
</footer>
<script>
window.useThisGithubPath = 'muaz-khan/DataChannel';
</script>
<!-- commits.js is useless for you! -->
<script src="https://cdn.webrtc-experiment.com/commits.js" async> </script>
</body>
</html>