This commit is contained in:
yumoqing 2024-10-11 15:53:30 +08:00
parent 1bd3db21d3
commit 595e2c126f

View File

@ -325,6 +325,7 @@ bricks.RTCP2PConnect = class {
async connection_statechange(peer, event){ async connection_statechange(peer, event){
var pc = this.peers[peer.id].pc; var pc = this.peers[peer.id].pc;
var role = this.peers[peer.id].role;
console.log(`${peer.id} state changed. new state=${pc.connectionState}`); console.log(`${peer.id} state changed. new state=${pc.connectionState}`);
if (pc.connectionState == 'disconnected'){ if (pc.connectionState == 'disconnected'){
var w = new bricks.Error({ var w = new bricks.Error({
@ -336,18 +337,15 @@ bricks.RTCP2PConnect = class {
return; return;
} }
if (pc.connectionState == 'connected'){ if (pc.connectionState == 'connected'){
if (this.opts.data_connect){ if (this.opts.data_connect && role == 'requester'){
var dc = pc.createDataChannel(); var dc = pc.createDataChannel();
if (this.requester && this.opts.data_connect){ await this.dc_created(peer, dc);
await this.createDataChannel(peer);
}
} }
} }
} }
async createDataChannel(peer){ async dc_created(peer, dc){
var pc = this.peers[peer.id].pc; console.log('dc_created.....');
dc = pc.createDataChannel('data');
this.peers[peer.id].dc = dc; this.peers[peer.id].dc = dc;
dc.onmessage = this.datachannel_message.bind(this, peer); dc.onmessage = this.datachannel_message.bind(this, peer);
dc.onopen = this.datachannel_open(this, peer); dc.onopen = this.datachannel_open(this, peer);
@ -355,18 +353,21 @@ bricks.RTCP2PConnect = class {
} }
async datachannel_message(peer, msg){ async datachannel_message(peer, msg){
console.log('datachannel_message():', peer. msg);
var dc = this.peers[peer.id].dc; var dc = this.peers[peer.id].dc;
if (this.opts.on_dc_messaage){ if (this.opts.on_dc_messaage){
await this.opts.on_dc_message(dc, msg); await this.opts.on_dc_message(dc, msg);
} }
} }
async datachannel_open(peer){ async datachannel_open(peer){
console.log('datachannel_open():', arguments, peer);
var dc = this.peers[peer.id].dc var dc = this.peers[peer.id].dc
if (this.opts.on_dc_open){ if (this.opts.on_dc_open){
await this.opts.on_dc_open(dc); await this.opts.on_dc_open(dc);
} }
} }
async datachannel_close(peer){ async datachannel_close(peer){
console.log('datachannel_close():', peer);
var dc = this.peers[peer.id].dc var dc = this.peers[peer.id].dc
if (this.opts.on_dc_close){ if (this.opts.on_dc_close){
await this.opts.on_dc_close(dc); await this.opts.on_dc_close(dc);
@ -394,10 +395,7 @@ bricks.RTCP2PConnect = class {
pc.onconnectionstatechange = this.connection_statechange.bind(this, peer); pc.onconnectionstatechange = this.connection_statechange.bind(this, peer);
pc.ondatachanel = event => { pc.ondatachanel = event => {
var dc = event.channel; var dc = event.channel;
this.peers[peer.id].dc = dc; await this.dc_created(peer, dc);
dc.onmessage = this.data_message_recv.bind(this, peer);
dc.onopen = this.datachannel_opened(this, peer);
dc.onclose = this.datachannl_closed(this, peer);
} }
pc.ontrack = event => { pc.ontrack = event => {
remoteVideo.set_stream(event.streams[0]); remoteVideo.set_stream(event.streams[0]);
@ -409,7 +407,7 @@ bricks.RTCP2PConnect = class {
var mediaOptions = this.opts.media_options; var mediaOptions = this.opts.media_options;
this.local_stream = await navigator.mediaDevices.getUserMedia(mediaOptions); this.local_stream = await navigator.mediaDevices.getUserMedia(mediaOptions);
this.localVideo = new bricks.VideoBox(); this.localVideo = new bricks.VideoBox();
this.localVideo.set_stream(local_stream); this.localVideo.set_stream(this.local_stream);
} catch (error) { } catch (error) {
console.error('获取本地媒体流失败:', error); console.error('获取本地媒体流失败:', error);
} }