我试图发送文本给我的所有同行,我发现这个函数"sendDirectlyToAll“。
为了方便您,我将函数信息放在这里:
sendDirectlyToAll(channelLabel,messageType,有效载荷)--通过dataChannel向房间里的所有同行广播一条消息。 字符串channelLabel - dataChannel发送的标签。 字符串messageType -发送的消息类型的键。 对象有效负载-要发送到对等点的任意值或对象。
我不明白第二和第三参数的含义。你能给我举一个如何使用这个功能的例子吗?
谢谢德里克
发布于 2017-02-22 14:39:03
下面是我的示例,展示了我是如何使它工作的:
/**
* 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);
}
}
}此外,官方的SimpleWebRTC问题跟踪器:https://github.com/andyet/SimpleWebRTC/issues/450上也有这个问题的答案。
请参阅我在这个示例中的博客文章:https://blog.ueffing.net/post/2017/02/22/simplewebrtc-usage-example-of-senddirectlytoall/
https://stackoverflow.com/questions/37891029
复制相似问题