This commit is contained in:
yumoqing 2024-10-07 13:57:30 +08:00
parent 4d4e217c75
commit 8b39dfd3e5

View File

@ -13,12 +13,15 @@ bricks.Signaling = class {
this.info = opts.info; this.info = opts.info;
this.peers = []; this.peers = [];
this.sessions = {}; this.sessions = {};
this.handlers = {};
this.sessionhandlers = {};
this.init_websocket(); this.init_websocket();
} }
init_websocket(){ init_websocket(){
this.socket = new WebSocket(this.signaling_url); this.socket = new WebSocket(this.signaling_url);
this.socket.onmessage = this.signaling_message_handle.bind(this); this.socket.onmessage = this.signal_recvdata.bind(this);
this.socket.onopen = this.signaling_login.bind(this); this.socket.onopen = this.login.bind(this);
this.socket.onclose = function(){ this.socket.onclose = function(){
this.init_websockset(); this.init_websockset();
}; };
@ -28,8 +31,57 @@ bricks.Signaling = class {
} catch(e){ } catch(e){
console.log('error,', e); console.log('error,', e);
} }
this.init_websocket();
}; };
} }
async signaling_recvdata(event){
var datapkg = JSON.parse(event.data);
var data = datapkg.data;
var name = data.sessionid
var handler = this.handlers.get(name)
if (!handler){
handler = this.recvdata_handler.bind(this)
}
await handler(data);
}
add_handler(key, handler){
this.handlers[key] = handler;
}
add_sessionhandler(sessiontype, handler){
self.sessionhandlers[sessiontype] = handler;
}
async recvdata_handler(data){
if (data.type == 'login'){
this.peers[data.msgfrom.id] = data.msgfrom;
return;
if (data.type == 'logout'){
var peers = {};
Object.keys(this.peers).forEach(k =>{
if (k != data.msgfrom.id){
peers[k] = this.peers[k];
}
});
}
if (data.type == 'new_session'){
}
}
new_session(sessiontype, opts){
var k = this.sessionhandlers.get(sessiontype);
if (!k){
throw 'new_session() exception(' + sessiontype + ')';
}
var d = {
type:'new_session',
session: {
sessiontype:sessiontype,
sessionid:bricks.uuid()
}
};
this.send_data(d);
var h = new k(this, sessiontype, opts);
this.add_handler(sessionid, h.recvdata_handler);
return h
}
login(){ login(){
var d = { var d = {
type:'login', type:'login',
@ -44,7 +96,7 @@ bricks.Signaling = class {
this.send_data(d); this.send_data(d);
} }
send_data(d){ send_data(d){
d.from = this.info; d.msgfrom = this.info;
var s = JSON.stringify(d); var s = JSON.stringify(d);
self.socket.send(s); self.socket.send(s);
} }
@ -343,10 +395,10 @@ bricks.RTCClient = class extends bricks.VBox {
break; break;
case 'callRequest': case 'callRequest':
if (this.auto_call_accept){ if (this.auto_call_accept){
this.call_accepted(d.from); this.call_accepted(d.msgfrom);
return return
} }
this.need_conform_call(d.from); this.need_conform_call(d.msgfrom);
console.log('callRequest branch exe'); console.log('callRequest branch exe');
break; break;
case 'offer': case 'offer':
@ -366,15 +418,15 @@ bricks.RTCClient = class extends bricks.VBox {
} }
break; break;
case 'answer': case 'answer':
console.log('#### receive_answer: d.from=', d.from, 'peer_info=', this.peer_info); console.log('#### receive_answer: d.msgfrom=', d.msgfrom, 'peer_info=', this.peer_info);
if (d.from.id == this.peer_info.id){ if (d.msgfrom.id == this.peer_info.id){
var desc = new RTCSessionDescription(d.answer); var desc = new RTCSessionDescription(d.answer);
await this.pc.setRemoteDescription(desc); await this.pc.setRemoteDescription(desc);
console.log('answer branch exe'); console.log('answer branch exe');
} }
break; break;
case 'iceCandidate': case 'iceCandidate':
if (d.from.id == this.peer_info.id){ if (d.msgfrom.id == this.peer_info.id){
var candidate = new RTCIceCandidate(d.candidate); var candidate = new RTCIceCandidate(d.candidate);
await this.pc.addIceCandidate(candidate); await this.pc.addIceCandidate(candidate);
console.log('iceCandidate branch exe'); console.log('iceCandidate branch exe');