SimpleWebRTC: usage example of sendDirectlyToAll
2017-02-22

2018-04-22 12:30:19 WebRTC, Javascript

How to send text or data to all peers in SimpleWebRTC by using sendDirectlyToAll method.

Here is my usage example showing how i managed to get it working:

/**
* send directly to all other peers
*/
oSimpleWebRTC.sendDirectlyToAll(
    'meta',         // sLabel
    'info',         // sType - will become oData.sType
    {"foo": "bar"}  // oData - will become oData.payload
);

/**
* Handle incoming dataChannel messages sent by "sendDirectlyToAll"
* @param {object} oPeer The Remote sending Peer Object
* @param {string} sLabel A Label, e.g.: 'meta'
* @param {object} oData Object containing the relevant Data
*/
oSimpleWebRTC.on('channelMessage', function (oPeer, sLabel, oData) {

    // e.g. we want label "hark" to be ignored, as it fires continiously.
    if ('hark' === sLabel) {
        return true;
    }

    if ('meta' === sLabel) {

        if ('info' === oData.type)
        {
            // do your stuff
            console.log(oData.payload.foo);
        }
    }
}

Links