|
@@ -1,14 +1,14 @@
|
|
|
|
+import { useWebRTCStore } from "@/stores/modules/webrtcStore";
|
|
import { MSG_TYPE, MSG_TYPE_MAP } from "@/common/constant/msgType";
|
|
import { MSG_TYPE, MSG_TYPE_MAP } from "@/common/constant/msgType";
|
|
import * as Constant from "@/common/constant/Constant";
|
|
import * as Constant from "@/common/constant/Constant";
|
|
|
|
|
|
// 发送消息处理
|
|
// 发送消息处理
|
|
-export const setMessageHook = (message, state) => {
|
|
|
|
|
|
+export const setMessageHook = (message, state) => {
|
|
// 文本消息
|
|
// 文本消息
|
|
if (message.contentType == MSG_TYPE.TEXT) {
|
|
if (message.contentType == MSG_TYPE.TEXT) {
|
|
state.messages.push({
|
|
state.messages.push({
|
|
...message,
|
|
...message,
|
|
toUsername: message.friendUsername,
|
|
toUsername: message.friendUsername,
|
|
-
|
|
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
@@ -71,11 +71,64 @@ export const handleMessageHook = (message, state) => {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
// 音频通话
|
|
// 音频通话
|
|
- if (message.contentType === MSG_TYPE.AUDIO_ONLINE) {
|
|
|
|
- if (message.contentType >= Constant.DIAL_MEDIA_START && messagePB.contentType <= Constant.DIAL_MEDIA_END) {
|
|
|
|
- this.dealMediaCall(messagePB);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- const { type, sdp, iceCandidate } = JSON.parse(messagePB.content);
|
|
|
|
|
|
+ if (message.type === Constant.MESSAGE_TRANS_TYPE) {
|
|
|
|
+ if (
|
|
|
|
+ message.contentType >= Constant.DIAL_MEDIA_START &&
|
|
|
|
+ message.contentType <= Constant.DIAL_MEDIA_END
|
|
|
|
+ ) {
|
|
|
|
+ // 媒体通话处理
|
|
|
|
+ // dealMediaCall(message);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ const rtcStore = useWebRTCStore();
|
|
|
|
+ const { type, sdp, iceCandidate } = JSON.parse(message.content);
|
|
|
|
+ // 接收answer:设置对端sdp
|
|
|
|
+ if (type === "answer") {
|
|
|
|
+ const answerSdp = new RTCSessionDescription({ type, sdp });
|
|
|
|
+ rtcStore.setRemoteDescription(answerSdp);
|
|
|
|
+ }
|
|
|
|
+ // 添加answer ice
|
|
|
|
+ if (type === "answer_ice") {
|
|
|
|
+ rtcStore.addIceCandidate(iceCandidate);
|
|
|
|
+ }
|
|
|
|
+ //
|
|
|
|
+ if (type === "offer_ice") {
|
|
|
|
+ rtcStore.addIceCandidate(iceCandidate);
|
|
|
|
+ }
|
|
|
|
+ // 响应对端offer
|
|
|
|
+ if (type === "offer") {
|
|
|
|
+ // 检查媒体权限是否开启
|
|
|
|
+
|
|
|
|
+ // let preview = null;
|
|
|
|
+ let video = false;
|
|
|
|
+ if (message.contentType === Constant.VIDEO_ONLINE) {
|
|
|
|
+ // preview = document.getElementById("localVideoReceiver");
|
|
|
|
+ video = true;
|
|
|
|
+ // // 屏幕共享
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (message.contentType === Constant.AUDIO_ONLINE) {
|
|
|
|
+ // preview = document.getElementById("audioPhone");
|
|
|
|
+ // 音频电话
|
|
|
|
+ }
|
|
|
|
+ // 获取媒体流
|
|
|
|
+ navigator.mediaDevices
|
|
|
|
+ .getUserMedia({
|
|
|
|
+ audio: true,
|
|
|
|
+ video: video,
|
|
|
|
+ })
|
|
|
|
+ .then(async (stream) => {
|
|
|
|
+ // preview.srcObject = stream;
|
|
|
|
+ rtcStore.addLocalStream(stream);
|
|
|
|
+ const answer = await rtcStore.createOffer();
|
|
|
|
+ // 发送 offer 给对等端
|
|
|
|
+ let data = {
|
|
|
|
+ content: JSON.stringify(answer),
|
|
|
|
+ type: Constant.MESSAGE_TRANS_TYPE,
|
|
|
|
+ messageType: message.contentType,
|
|
|
|
+ };
|
|
|
|
+ state.sendMessage(data);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
}
|
|
}
|
|
};
|
|
};
|