SimpleWebRTC: usage example of sendDirectlyToAll

2017-02-22 2021-09-19 11:45:52 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


This website uses Cookies to provide you with the best possible service. Please see our Privacy Policy for more information. Click the check box below to accept cookies. Then confirm with a click on "Save".