Compare commits
2 Commits
7c393f0541
...
2f5f1f7f2a
Author | SHA1 | Date | |
---|---|---|---|
2f5f1f7f2a | |||
58761c75e2 |
@ -71,7 +71,7 @@ interface LyricItem {
|
|||||||
export default function Controls(): JSX.Element {
|
export default function Controls(): JSX.Element {
|
||||||
const ref = useRef<HTMLDialogElement | null>(null);
|
const ref = useRef<HTMLDialogElement | null>(null);
|
||||||
const [history, setHistory] = useState<LyricItem[]>([]);
|
const [history, setHistory] = useState<LyricItem[]>([]);
|
||||||
const { songId, songName, songArtist } = useContext(DanmuContext);
|
const { songId, songName, songArtist, listener } = useContext(DanmuContext);
|
||||||
|
|
||||||
const { start, resume, pause, setLyrics, offset, setOffset } =
|
const { start, resume, pause, setLyrics, offset, setOffset } =
|
||||||
useLyricsRunner((line) => {
|
useLyricsRunner((line) => {
|
||||||
@ -82,6 +82,12 @@ export default function Controls(): JSX.Element {
|
|||||||
},
|
},
|
||||||
...items,
|
...items,
|
||||||
]);
|
]);
|
||||||
|
if (listener) {
|
||||||
|
// 测试用
|
||||||
|
// listener('666');
|
||||||
|
// 实际输出歌词
|
||||||
|
listener(line.slice(0, 10));
|
||||||
|
}
|
||||||
console.log(line);
|
console.log(line);
|
||||||
}, 200);
|
}, 200);
|
||||||
|
|
||||||
|
@ -1,15 +1,34 @@
|
|||||||
"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";
|
||||||
|
|
||||||
|
const useGenMessage = () => {
|
||||||
|
const [msgId, setMsgId] = useState(3);
|
||||||
|
return {
|
||||||
|
genMessage(text: string) {
|
||||||
|
const msg = {
|
||||||
|
msg_id: msgId,
|
||||||
|
client_time: Date.now(),
|
||||||
|
type: "Bili",
|
||||||
|
data: { text },
|
||||||
|
};
|
||||||
|
setMsgId(msgId + 1);
|
||||||
|
return msg;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
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, dispatch } = useContext(DanmuContext);
|
||||||
|
const { genMessage } = useGenMessage();
|
||||||
|
|
||||||
const { sendMessage, lastMessage, readyState } = useWebSocket(MOMO_IM_URL, {
|
const { sendMessage, lastMessage, readyState } = useWebSocket(MOMO_IM_URL, {
|
||||||
heartbeat: {
|
heartbeat: {
|
||||||
@ -27,9 +46,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 +64,22 @@ 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([]);
|
||||||
|
dispatch({
|
||||||
|
type: "listener",
|
||||||
|
payload: {
|
||||||
|
listener: (msg: string) => {
|
||||||
|
const data = genMessage(msg);
|
||||||
|
sendMessage(JSON.stringify(data));
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}, [readyState]);
|
}
|
||||||
|
}, [readyState, roomId, momoId, momoToken]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (lastMessage !== null) {
|
if (lastMessage !== null) {
|
||||||
|
@ -7,6 +7,7 @@ export interface DanmuPayload {
|
|||||||
songId?: string;
|
songId?: string;
|
||||||
songName?: string;
|
songName?: string;
|
||||||
songArtist?: string;
|
songArtist?: string;
|
||||||
|
listener?: (msg: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DanmuAction {
|
export interface DanmuAction {
|
||||||
@ -30,6 +31,8 @@ const reducer = (state: DanmuPayload, action: DanmuAction): DanmuPayload => {
|
|||||||
};
|
};
|
||||||
case "lives":
|
case "lives":
|
||||||
return { ...state, liveId: payload.liveId, liveName: payload.liveName };
|
return { ...state, liveId: payload.liveId, liveName: payload.liveName };
|
||||||
|
case "listener":
|
||||||
|
return { ...state, listener: payload.listener };
|
||||||
default:
|
default:
|
||||||
throw new Error("not support action");
|
throw new Error("not support action");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user