2026-05-19 15:55:02 +08:00
|
|
|
|
import VERTC, {
|
|
|
|
|
|
RoomProfileType,
|
|
|
|
|
|
StreamIndex,
|
|
|
|
|
|
} from "@volcengine/rtc";
|
|
|
|
|
|
|
|
|
|
|
|
let engine = null;
|
|
|
|
|
|
|
|
|
|
|
|
let currentRemoteUserId = null;
|
|
|
|
|
|
|
|
|
|
|
|
let isJoining = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// =========================
|
|
|
|
|
|
// 创建单例 engine
|
|
|
|
|
|
// =========================
|
|
|
|
|
|
|
|
|
|
|
|
function getEngine(appId) {
|
|
|
|
|
|
if (!engine) {
|
|
|
|
|
|
engine = VERTC.createEngine(appId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return engine;
|
2026-05-11 14:59:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-19 15:55:02 +08:00
|
|
|
|
|
|
|
|
|
|
// =========================
|
|
|
|
|
|
// 加入房间
|
|
|
|
|
|
// =========================
|
|
|
|
|
|
|
|
|
|
|
|
export async function joinRoom(
|
|
|
|
|
|
container,
|
|
|
|
|
|
appId,
|
|
|
|
|
|
roomId,
|
|
|
|
|
|
token,
|
|
|
|
|
|
userId
|
|
|
|
|
|
) {
|
|
|
|
|
|
if (isJoining) {
|
|
|
|
|
|
console.warn("正在 joinRoom,跳过");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
isJoining = true;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log("joinRoom start");
|
|
|
|
|
|
|
|
|
|
|
|
// 先离开旧房间
|
|
|
|
|
|
await leaveRoom();
|
|
|
|
|
|
|
|
|
|
|
|
const rtcEngine = getEngine(appId);
|
|
|
|
|
|
|
|
|
|
|
|
// 防止重复监听
|
|
|
|
|
|
rtcEngine.removeAllListeners();
|
|
|
|
|
|
|
|
|
|
|
|
// 监听远端流
|
|
|
|
|
|
rtcEngine.on(
|
|
|
|
|
|
VERTC.events.onUserPublishStream,
|
|
|
|
|
|
async (e) => {
|
|
|
|
|
|
console.log("收到远端流", e);
|
|
|
|
|
|
|
|
|
|
|
|
currentRemoteUserId = e.userId;
|
|
|
|
|
|
|
|
|
|
|
|
if (!container) return;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 清空旧 video
|
|
|
|
|
|
container.innerHTML = "";
|
|
|
|
|
|
|
|
|
|
|
|
// 隐藏本地
|
|
|
|
|
|
await rtcEngine.setUserVisibility(false);
|
|
|
|
|
|
|
|
|
|
|
|
// 播放远端
|
|
|
|
|
|
rtcEngine.setRemoteVideoPlayer(
|
|
|
|
|
|
StreamIndex.STREAM_INDEX_MAIN,
|
|
|
|
|
|
{
|
|
|
|
|
|
userId: e.userId,
|
|
|
|
|
|
renderDom: container,
|
|
|
|
|
|
visible: true,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 加入房间
|
|
|
|
|
|
await rtcEngine.joinRoom(
|
|
|
|
|
|
token,
|
|
|
|
|
|
roomId,
|
|
|
|
|
|
{ userId },
|
|
|
|
|
|
{
|
|
|
|
|
|
isAutoPublish: false,
|
|
|
|
|
|
isAutoSubscribeAudio: false,
|
|
|
|
|
|
isAutoSubscribeVideo: true,
|
|
|
|
|
|
roomProfileType:
|
|
|
|
|
|
RoomProfileType.meeting,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
console.log("joinRoom success");
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error("joinRoom failed", err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
isJoining = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// =========================
|
|
|
|
|
|
// 离开房间
|
|
|
|
|
|
// =========================
|
|
|
|
|
|
|
2026-05-11 14:59:36 +08:00
|
|
|
|
export async function leaveRoom() {
|
2026-05-19 15:55:02 +08:00
|
|
|
|
if (!engine) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
console.log("leaveRoom start");
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 移除事件
|
|
|
|
|
|
engine.removeAllListeners();
|
|
|
|
|
|
|
|
|
|
|
|
// 停止远端渲染
|
|
|
|
|
|
if (currentRemoteUserId) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
engine.setRemoteVideoPlayer(
|
|
|
|
|
|
StreamIndex.STREAM_INDEX_MAIN,
|
|
|
|
|
|
{
|
|
|
|
|
|
userId: currentRemoteUserId,
|
|
|
|
|
|
renderDom: null,
|
|
|
|
|
|
visible: false,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 离开房间
|
|
|
|
|
|
try {
|
|
|
|
|
|
await engine.leaveRoom();
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 等待 SDK 内部释放
|
|
|
|
|
|
await new Promise((resolve) =>
|
|
|
|
|
|
setTimeout(resolve, 500)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
currentRemoteUserId = null;
|
|
|
|
|
|
|
|
|
|
|
|
console.log("leaveRoom success");
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
}
|
2026-05-11 14:59:36 +08:00
|
|
|
|
}
|