69 lines
2.0 KiB
JavaScript
69 lines
2.0 KiB
JavaScript
// 导入类(ES6 模块化)
|
||
import { webSocket_ModuleSimple } from "./newWebRtcSync.js";
|
||
|
||
class rtcCallee {
|
||
constructor(userIdentity, user_id) {
|
||
this.current_time = new Date();
|
||
this.identity = userIdentity;
|
||
// websocket对象
|
||
this.wsClient = null;
|
||
// WebRTC 核心对象
|
||
this.rtcPeer = null;
|
||
this.dataChannel = null;
|
||
this.state;
|
||
// webRTC 信令
|
||
this.clientCandidates = [];
|
||
this.remoteOffer;
|
||
this.localAnswer;
|
||
this.localStream;
|
||
this.remoteStream;
|
||
this._init_ws(user_id);
|
||
}
|
||
_init_ws(user_id) {
|
||
// websocket初始化
|
||
if (this.wsClient) {
|
||
this.wsClient = null;
|
||
}
|
||
console.info(`初始化websocket`);
|
||
// const wsService = `ws://localhost:8000/wsSignaling/${user_id}/`
|
||
const wsService = `wss://api.vlos.net/wsSignaling/${user_id}/`;
|
||
this.wsClient = new webSocket_ModuleSimple(wsService);
|
||
this.wsClient.connect();
|
||
this.handlerWsSignalingLisioner();
|
||
}
|
||
/**
|
||
* 监听websocket信息 回调函数方式监控websocket数据
|
||
*/
|
||
async handlerWsSignalingLisioner() {
|
||
this.wsClient.on({
|
||
onMessage: async (msg) => {
|
||
const res_dict = JSON.parse(msg);
|
||
console.info(
|
||
`[exportWebRtcCallee.js]->[handlerWsSignalingLisioner函数][${this.identity}][${this.current_time.toLocaleTimeString()}]>>>收到信息`,
|
||
"type:",
|
||
res_dict.type,
|
||
"长度",
|
||
msg.length,
|
||
);
|
||
switch (res_dict.type) {
|
||
case "candidate":
|
||
console.info(
|
||
`[exportWebRtcCallee.js][${this.identity}][${this.current_time.toLocaleTimeString()}]>>>监听candidate`,
|
||
res_dict.data,
|
||
);
|
||
break;
|
||
case "offer":
|
||
this.remoteOffer = res_dict.data;
|
||
console.info(
|
||
`[exportWebRtcCallee.js][${this.identity}][${this.current_time.toLocaleTimeString()}]>>>收到远程offer`,
|
||
);
|
||
console.warn(`收到的offer,`, this.remoteOffer);
|
||
break;
|
||
}
|
||
},
|
||
});
|
||
}
|
||
}
|
||
|
||
new rtcCallee("callee", 123);
|