DataChannel/simple.html
HOME © Muaz Khan . @WebRTCWeb . Github . Latest issues . What's New?
Open
Join
|
|
|
<script src="https://cdn.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!
<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
};
// on file successfully sent
channel. **onFileSent** = function (file) {
// file.name
// file.size
};
// on file successfully received
channel. **onFileReceived** = function (fileName) {};
</script>
<script>
// error to open data ports
channel. **onerror** = function(event) {}
// data ports suddenly dropped
channel. **onclose** = function(event) {}
</script>
<script>
// by default socket.io 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>
Send Message