2

I am trying to send text to all my peers and I found this function "sendDirectlyToAll".

For your convenience, I put the function information here:


sendDirectlyToAll(channelLabel, messageType, payload) - broadcasts a message to all peers in the room via a dataChannel.

string channelLabel - the label for the dataChannel to send on.

string messageType - the key for the type of message being sent.

object payload - an arbitrary value or object to send to peers.


I do not understand the meaning of 2nd and 3rd parameters. Could you kindly show me an example how to use this function?

Thanks Derek

Community
  • 1
  • 1
derek
  • 9,358
  • 11
  • 53
  • 94

1 Answers1

0

Here is my 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);
        }
    }
}

Also, There are Answers to this question at the official SimpleWebRTC issues Tracker: https://github.com/andyet/SimpleWebRTC/issues/450

See my blog post to this example: https://blog.ueffing.net/post/2017/02/22/simplewebrtc-usage-example-of-senddirectlytoall/