From 58761c75e29bc237d09d05c5851f43aac7f68f04 Mon Sep 17 00:00:00 2001 From: sealday Date: Sun, 7 Apr 2024 04:49:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E6=88=BF=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/danmu/danmu.tsx | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/app/danmu/danmu.tsx b/app/danmu/danmu.tsx index d2128ad..c3c1f58 100644 --- a/app/danmu/danmu.tsx +++ b/app/danmu/danmu.tsx @@ -1,15 +1,17 @@ "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 MessageDispatcher from "./message"; import { Token } from "@/lib/types"; import dayjs from "dayjs"; +import { DanmuContext } from "./provider"; const MOMO_IM_URL = "wss://live-ws.immomo.com/ws/im"; export default function Danmu({ token }: { token: Token }) { const [messageHistory, setMessageHistory] = useState[]>([]); const ref = useRef(null); + const { liveId, liveName } = useContext(DanmuContext); const { sendMessage, lastMessage, readyState } = useWebSocket(MOMO_IM_URL, { heartbeat: { @@ -27,28 +29,31 @@ export default function Danmu({ token }: { token: Token }) { }); const momoId = token.uid; - const roomId = "1476810196216"; + const roomId = liveId; const momoToken = token.token; - const clientTime = Date.now(); - const data = { - msg_id: 1, - client_time: clientTime, - type: "Sauth", - data: { - momoid: momoId, - roomid: roomId, - role: 6, - isVisitor: false, - token: momoToken, - }, - }; - useEffect(() => { if (ReadyState.OPEN === readyState) { - sendMessage(JSON.stringify(data)); + const clientTime = Date.now(); + const data = { + msg_id: 1, + client_time: clientTime, + type: "Sauth", + data: { + momoid: momoId, + roomid: roomId, + role: 6, + isVisitor: false, + token: momoToken, + }, + }; + if (roomId) { + sendMessage(JSON.stringify(data)); + console.log('重新连接...'); + setMessageHistory([]); + } } - }, [readyState]); + }, [readyState, roomId, momoId, momoToken]); useEffect(() => { if (lastMessage !== null) {