0
点赞
收藏
分享

微信扫一扫

webrtc RTCPeerConnection前端使用记录

年夜雪 2023-02-03 阅读 123

参考资料

​​https://developer.mozilla.org/zh-CN/docs/Web/API/RTCPeerConnection​​​​https://developer.mozilla.org/zh-CN/docs/Web/API/RTCPeerConnection#createdatachannel
https://developer.mozilla.org/zh-CN/docs/Web/API/RTCPeerConnection/createDataChannel
https://www.jianshu.com/p/4cce381994e2​​

问题

前后端进行非视频音频信息交互

createDataChannel
RTCDataChannel createDataChannel (DOMString label, optional RTCDataChannelInit dataChannelDict);

通过 peerconnection 建立一条数据信道,用于发送非视频音频信息。

例子

var pc = new PeerConnection();
var channel = pc.createDataChannel("Mydata");
channel.onopen = function(event) {
channel.send('sending a message');
}
channel.onmessage = function(event) { console.log(event.data); }

思考

前端使用的变量pc.createDataChannel失败,原因是交互的方式采用了websocket,并不是已有的链接

    this.sdp = '';
this.candidate = '';
let trace = this.trace;
try { if (this.pc) this.pc.close(); if (this.ws) this.ws.close(); } catch (e) {}
this.pc = null;
this.ws = new WebSocket(location.protocol.replace('http', 'ws') + '//' + location.host + '/?serv=' + this.serv + '&args=' + this.args);

举报

相关推荐

0 条评论