|
@@ -1,5 +1,5 @@
|
|
|
import { defineStore } from "pinia";
|
|
|
-import { MSG_TYPE, MESSAGE_TYPE_USER } from "@/common/constant/msgType";
|
|
|
+import { MSG_TYPE, MESSAGE_TYPE_USER, MESSAGE_TYPE_GROUP } from "@/common/constant/msgType";
|
|
|
import { useWebSocketStore } from "@/stores/modules/webSocketStore";
|
|
|
|
|
|
export const useWebRTCStore = defineStore("webrtc", {
|
|
@@ -24,13 +24,12 @@ export const useWebRTCStore = defineStore("webrtc", {
|
|
|
// 可以添加更多 STUN/TURN 服务器
|
|
|
],
|
|
|
},
|
|
|
- }),
|
|
|
- webSocketStore: useWebSocketStore(),
|
|
|
+ }),
|
|
|
actions: {
|
|
|
// 初始化 WebRTC 连接
|
|
|
- initConnection(MESSAGE_TYPE, data) {
|
|
|
+ initConnection(MESSAGE_TYPE) {
|
|
|
this.cleanup();
|
|
|
-
|
|
|
+ const wsStore = useWebSocketStore();
|
|
|
try {
|
|
|
this.peerConnection = new RTCPeerConnection();
|
|
|
|
|
@@ -42,12 +41,11 @@ export const useWebRTCStore = defineStore("webrtc", {
|
|
|
type: "offer_ice",
|
|
|
iceCandidate: event.candidate,
|
|
|
};
|
|
|
- this.$webSocketStore.sendMessage({
|
|
|
+ wsStore.sendMessage({
|
|
|
contentType: MSG_TYPE.AUDIO_ONLINE,
|
|
|
type: "webrtc",
|
|
|
messageType: MESSAGE_TYPE,
|
|
|
content: JSON.stringify(candidate),
|
|
|
- ...data,
|
|
|
});
|
|
|
}
|
|
|
if (MESSAGE_TYPE === MESSAGE_TYPE_GROUP) {
|
|
@@ -55,15 +53,18 @@ export const useWebRTCStore = defineStore("webrtc", {
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
+ // 监听 ICE 状态变化
|
|
|
this.peerConnection.onconnectionstatechange = () => {
|
|
|
this.connectionState = this.peerConnection.connectionState;
|
|
|
};
|
|
|
|
|
|
+ // 当连接成功后,从里面获取语音视频流: 监听 ICE candidate:包含语音视频流
|
|
|
this.peerConnection.ontrack = (event) => {
|
|
|
+ // 添加远程媒体流
|
|
|
if (!this.remoteStream) {
|
|
|
this.remoteStream = new MediaStream();
|
|
|
}
|
|
|
+ // 添加远程媒体流
|
|
|
event.streams[0].getTracks().forEach((track) => {
|
|
|
this.remoteStream.addTrack(track);
|
|
|
});
|