diff --git a/DataChannel/DataChannel.js b/DataChannel/DataChannel.js
index 50582499..bf303af0 100644
--- a/DataChannel/DataChannel.js
+++ b/DataChannel/DataChannel.js
@@ -93,19 +93,23 @@
if (self.config) return;
self.config = {
- onroom: function(room) {
+ ondatachannel: function(room) {
if (!dataConnector) {
self.room = room;
return;
}
+ var tempRoom = {
+ id: room.roomToken,
+ owner: room.broadcaster
+ };
+
+ if (self.ondatachannel) return self.ondatachannel(tempRoom);
+
if (self.joinedARoom) return;
self.joinedARoom = true;
- dataConnector.joinRoom({
- roomToken: room.roomToken,
- joinUser: room.broadcaster
- });
+ self.join(tempRoom);
},
onopen: function(userid, _channel) {
self.onopen(userid, _channel);
@@ -153,7 +157,7 @@
fileReceiver = new FileReceiver();
textReceiver = new TextReceiver();
- if (self.room) self.config.onroom(self.room);
+ if (self.room) self.config.ondatachannel(self.room);
}
this.open = function(_channel) {
@@ -175,6 +179,18 @@
prepareInit(init);
};
+ // manually join a room
+ this.join = function(room) {
+ if (!room.id || !room.owner) {
+ throw 'Invalid room info passed.';
+ }
+
+ dataConnector.joinRoom({
+ roomToken: room.id,
+ joinUser: room.owner
+ });
+ };
+
this.send = function(data, _channel) {
if (!data) throw 'No file, data or text message to share.';
if (data.size)
@@ -515,7 +531,7 @@
onmessage: function(response) {
if (response.userToken == self.userToken) return;
- if (isGetNewRoom && response.roomToken && response.broadcaster) config.onroom(response);
+ if (isGetNewRoom && response.roomToken && response.broadcaster) config.ondatachannel(response);
if (response.newParticipant) onNewParticipant(response.newParticipant);
@@ -627,7 +643,7 @@
packets = 0;
// uuid is used to uniquely identify sending instance
- var uuid = getRandomString();
+ file.uuid = getRandomString();
var reader = new window.FileReader();
reader.readAsDataURL(file);
@@ -636,7 +652,7 @@
function onReadAsDataURL(event, text) {
var data = {
type: 'file',
- uuid: uuid
+ uuid: file.uuid
};
if (event) {
@@ -649,7 +665,7 @@
remaining: packets--,
length: numberOfPackets,
sent: numberOfPackets - packets
- }, uuid);
+ }, file.uuid);
if (text.length > packetSize) data.message = text.slice(0, packetSize);
else {
@@ -657,7 +673,7 @@
data.last = true;
data.name = file.name;
- if (config.onFileSent) config.onFileSent(file, uuid);
+ if (config.onFileSent) config.onFileSent(file, file.uuid);
}
// WebRTC-DataChannels.send(data, privateDataChannel)
@@ -709,7 +725,7 @@
// if you don't want to auto-save to disk:
// channel.autoSaveToDisk=false;
if (root.autoSaveToDisk)
- FileSaver.SaveToDisk(virtualURL, data.name);
+ FileSaver.SaveToDisk(dataURL, data.name);
// channel.onFileReceived = function(fileName, file) {}
// file.blob || file.dataURL || file.url || file.uuid
@@ -885,7 +901,7 @@
credential: 'homeo',
username: 'homeo'
};
- iceServers.iceServers = [TURN, STUN];
+ iceServers.iceServers = [STUN, TURN];
}
var optional = {
diff --git a/DataChannel/README.md b/DataChannel/README.md
index fd80fb55..9b7117f2 100644
--- a/DataChannel/README.md
+++ b/DataChannel/README.md
@@ -89,6 +89,27 @@ channel.onmessage = function(message, userid, latency) { }
=
+##### `ondatachannel`
+
+Allows you show list of all available data channels to the user; and let him choose which one to join:
+
+```javascript
+channel.ondatachannel = function(data_channel) {
+ channel.join(data_channel);
+
+ // or
+ channel.join({
+ id: data_channel.id,
+ owner: data_channel.owner
+ });
+
+ // id: unique identifier for the session
+ // owner: unique identifier for the session initiator
+};
+```
+
+=
+
##### Use custom user-ids
```javascript
@@ -160,6 +181,17 @@ channel.leave(); // closing entire session
=
+##### `uuid` for files
+
+You can get `uuid` for each file (being sent) like this:
+
+```javascript
+channel.send(file);
+var uuid = file.uuid; // "file"-Dot-uuid
+```
+
+=
+
##### To Share files
```javascript
diff --git a/DataChannel/auto-session-establishment.html b/DataChannel/auto-session-establishment.html
index 45269bfa..444fccb2 100644
--- a/DataChannel/auto-session-establishment.html
+++ b/DataChannel/auto-session-establishment.html
@@ -298,8 +298,8 @@
Share Files
+ '';
}
- if (!parent) chatOutput.insertBefore(div, chatOutput.firstChild);
- else parent.insertBefore(div, parent.firstChild);
+ if (!parent) chatOutput.appendChild(div, chatOutput.firstChild);
+ else fileProgress.appendChild(div, fileProgress.firstChild);
div.tabIndex = 0;
div.focus();
diff --git a/DataChannel/index.html b/DataChannel/index.html
index 2e81632f..83aae0cc 100644
--- a/DataChannel/index.html
+++ b/DataChannel/index.html
@@ -1,452 +1,323 @@
-
+
+
+
+ DataChannel.js » A WebRTC Library for Data Sharing ® Muaz Khan
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Open New DataChannel Connection
+ Open
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Send Message Enter your email too; if you want "direct" reply!
+
+
+
+
+
+ Direct messages — to any user using his `user-id`
+ Eject/Reject any user — using his `user-id`
+ Leave any room (i.e. data session) or close entire session using `leave` method
+ File size is limitless!
+ Text message length is limitless!
+ Size of data is also limitless!
+ Fallback to firebase/socket.io/websockets/etc.
+ Users' presence detection using `onleave`
+ Latency detection
+ Multi-longest strings/files concurrent transmission
+
+
+
+
+
+
+<script src="https://www.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>
-
- File Sharing + Text Chat using WebRTC DataChannel ® Muaz Khan
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-↑
- WEBRTC EXPERIMENTS
-
-
-File Sharing + Text Chat using WebRTC DataChannel
-
-
-
- Copyright © 2013
- Muaz Khan < @muazkh >.
-
-
-
-
-
-
-
-
-
-
-
-Getting started with WebRTC DataChannel
-
-<script src="https://www.webrtc-experiment.com/DataChannel.js "></script>
-<script>
- var channel = new DataChannel ();
-
- // to create/open a new channel
- channel.open ('channel-name');
-
- // to send text/data or file
- channel.send (file || data || 'text');
-
- // if soemone already created a channel; to join it: use "connect" method
- channel.connect ('channel-name');
</script>
-
-Remember, A-to-Z, everything is optional! You can set channel-name in constructor or in
-open /connect methods. It is your choice!
-
-
-
-
-Features:
-
- Send file directly — of any size
- Send text-message of any length
- Send data directly
- Simplest syntax ever! Same like WebSockets.
- Supports fallback to socket.io/websockets/etc.
- Auto users' presence detection
- Allows you eject any user; or close your entire data session
-
-
-
-
-Additional:
-
+
<script>
- // to be alerted on data ports get open
- channel.onopen = function(userid) {}
-
- // to be alerted on data ports get new message
- channel.onmessage = function(message, userid) {}
-
- // by default; connection is [many-to-many]; you can use following directions
- channel.direction = 'one-to-one';
- channel.direction = 'one-to-many';
- channel.direction = 'many-to-many'; // --- it is default
-
- // show progress bar!
- channel.onFileProgress = function (packets) {
- // packets.remaining
- // packets.sent
- // packets.received
- // packets.length
+ var channel = new DataChannel('Session Unique Identifier');
+
+ channel.onopen = function(userid) {
+ chatInput.disabled = false;
+ chatInput.value = 'Hi, ' + userid;
+ chatInput.focus();
};
- // on file successfully sent
- channel.onFileSent = function (file) {
- // file.name
- // file.size
+ channel.onmessage = function(message, userid) {
+ chatOutput.innerHTML = userid + ': ' + message + '<hr />'
+ + chatOutput.innerHTML;
};
- // on file successfully received
- channel.onFileReceived = function (fileName) {};
+ channel.onleave = function(userid) {
+ chatOutput.innerHTML = userid + ' Left.<hr />'
+ + chatOutput.innerHTML;
+ };
</script>
-
-
-
-Errors Handling
-
-<script>
- // error to open data ports
- channel.onerror = function(event) {}
+
+
+
- // data ports suddenly dropped
- channel.onclose = function(event) {}
-</script>
-
-
-
-
-Use your own socket.io for signaling
-
-<script>
- // by default Firebase is used for signaling; you can override it
- channel.openSignalingChannel = 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>
-
-
-
-
- Feedback
-
-
-
-
- Send Message
-
-
-
-
-
-
\ No newline at end of file
+
+
+
+