feat: 支持切换房间
This commit is contained in:
parent
7c393f0541
commit
58761c75e2
@ -1,15 +1,17 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { useState, useEffect, useCallback, useRef } from "react";
|
import { useState, useEffect, useCallback, useRef, useContext } from "react";
|
||||||
import useWebSocket, { ReadyState } from "react-use-websocket";
|
import useWebSocket, { ReadyState } from "react-use-websocket";
|
||||||
import MessageDispatcher from "./message";
|
import MessageDispatcher from "./message";
|
||||||
import { Token } from "@/lib/types";
|
import { Token } from "@/lib/types";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
|
import { DanmuContext } from "./provider";
|
||||||
|
|
||||||
const MOMO_IM_URL = "wss://live-ws.immomo.com/ws/im";
|
const MOMO_IM_URL = "wss://live-ws.immomo.com/ws/im";
|
||||||
|
|
||||||
export default function Danmu({ token }: { token: Token }) {
|
export default function Danmu({ token }: { token: Token }) {
|
||||||
const [messageHistory, setMessageHistory] = useState<MessageEvent<any>[]>([]);
|
const [messageHistory, setMessageHistory] = useState<MessageEvent<any>[]>([]);
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
|
const { liveId, liveName } = useContext(DanmuContext);
|
||||||
|
|
||||||
const { sendMessage, lastMessage, readyState } = useWebSocket(MOMO_IM_URL, {
|
const { sendMessage, lastMessage, readyState } = useWebSocket(MOMO_IM_URL, {
|
||||||
heartbeat: {
|
heartbeat: {
|
||||||
@ -27,9 +29,11 @@ export default function Danmu({ token }: { token: Token }) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const momoId = token.uid;
|
const momoId = token.uid;
|
||||||
const roomId = "1476810196216";
|
const roomId = liveId;
|
||||||
const momoToken = token.token;
|
const momoToken = token.token;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (ReadyState.OPEN === readyState) {
|
||||||
const clientTime = Date.now();
|
const clientTime = Date.now();
|
||||||
const data = {
|
const data = {
|
||||||
msg_id: 1,
|
msg_id: 1,
|
||||||
@ -43,12 +47,13 @@ export default function Danmu({ token }: { token: Token }) {
|
|||||||
token: momoToken,
|
token: momoToken,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
if (roomId) {
|
||||||
useEffect(() => {
|
|
||||||
if (ReadyState.OPEN === readyState) {
|
|
||||||
sendMessage(JSON.stringify(data));
|
sendMessage(JSON.stringify(data));
|
||||||
|
console.log('重新连接...');
|
||||||
|
setMessageHistory([]);
|
||||||
}
|
}
|
||||||
}, [readyState]);
|
}
|
||||||
|
}, [readyState, roomId, momoId, momoToken]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (lastMessage !== null) {
|
if (lastMessage !== null) {
|
||||||
|
Loading…
Reference in New Issue
Block a user