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,28 +29,31 @@ 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;
|
||||||
|
|
||||||
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(() => {
|
useEffect(() => {
|
||||||
if (ReadyState.OPEN === readyState) {
|
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(() => {
|
useEffect(() => {
|
||||||
if (lastMessage !== null) {
|
if (lastMessage !== null) {
|
||||||
|
Loading…
Reference in New Issue
Block a user