RTCMultiConnection Docs

RTCMultiConnection API Reference / "send" method

"send" method can be used to send single or multiple files, single or multiple longer string messages, array-buffers or any other WebRTC data-channels compatible data type.
connection.send(file || data || 'longest-text-message');

Send Files

document.querySelector('input[type=file]').onchange = function() {
    connection.send(this.files[0]);
};

Send Data

connection.send({
    name: 'Muaz Khan',
    email: 'muazkh@gmail.com',
    twitter: 'https://twitter.com/muazkh',
    another: 'https://twitter.com/WebRTCWeb'
});

Send Array-Buffers

// Note: connection.preferJSON=true is added since v2.0

// Remember: it doesn't applies to file sharing.
// it applies to all other kinds of data.
connection.preferJSON = false;

// send array buffer
connection.send( new ArrayBuffer(10) );

// get array buffer
connection.onmessage =  function(event) {
    var buffer = event.data;
};

// convert string to array-buffer
connection.send( str2ab('a string test') );

// parsing array-buffer back into string
connection.onmessage =  function(event) {
    var string = ab2str(event.data);
};

// this method converts array-buffer into string
function ab2str(buf) {
    return String.fromCharCode.apply(null, new Uint16Array(buf));
}

// this method converts string into array-buffer
function str2ab(str) {
    var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
    var bufView = new Uint16Array(buf);
    for (var i = 0, strLen = str.length; i < strLen; i++) {
        bufView[i] = str.charCodeAt(i);
    }
    return buf;
}


Want to ask a Question?

You can include your email for private conversation!

Latest Updates