fix: chat emitter

This commit is contained in:
zijiren233 2024-08-31 11:17:13 +08:00
parent 414d798f3b
commit 163e6763e3
1 changed files with 44 additions and 23 deletions

View File

@ -51,6 +51,7 @@ let player: Artplayer;
const sendDanmuku = (msg: string) => {
if (!player || !player.plugins.artplayerPluginDanmuku) return;
player.plugins.artplayerPluginDanmuku.emit({
direct: true,
text: msg, //
color: "#fff", //
border: false //
@ -86,37 +87,42 @@ const sendElement = (msg: ElementMessage) => {
};
//
const msgList = ref<string[]>([]);
const sendText_ = ref("");
const sendText = () => {
if (sendText_.value.length === 0) {
return ElMessage({
const chatMsgList = ref<string[]>([]);
const sendChatText = (msg: string, onSuccess?: () => any, onFailed?: () => any) => {
if (msg.length === 0) {
ElMessage({
message: "发送的消息不能为空",
type: "warning"
});
if (onFailed) onFailed();
return;
}
strLengthLimit(sendText_.value, 4096);
strLengthLimit(msg, 4096);
sendElement(
ElementMessage.create({
type: ElementMessageType.CHAT_MESSAGE,
chatReq: sendText_.value
chatReq: msg
})
);
sendText_.value = "";
if (chatArea.value) chatArea.value.scrollTop = chatArea.value.scrollHeight;
if (onSuccess) onSuccess();
};
const sendChatMsg_ = ref("");
const onSendSuccess = () => {
sendChatMsg_.value = "";
};
const MAX_MESSAGE_COUNT = 64; //
const sendMsg = (msg: string) => {
msgList.value.push(msg);
chatMsgList.value.push(msg);
//
nextTick(() => {
if (msgList.value.length > MAX_MESSAGE_COUNT) {
msgList.value.splice(0, msgList.value.length - MAX_MESSAGE_COUNT);
if (chatMsgList.value.length > MAX_MESSAGE_COUNT) {
chatMsgList.value.splice(0, chatMsgList.value.length - MAX_MESSAGE_COUNT);
}
// sessionStorage
sessionStorage.setItem(`chatMessages-${roomID}`, JSON.stringify(msgList.value));
sessionStorage.setItem(`chatMessages-${roomID}`, JSON.stringify(chatMsgList.value));
});
//
@ -140,7 +146,14 @@ const playerOption = computed<options>(() => {
//
artplayerPluginDanmuku({
danmuku: [],
speed: 4
speed: 8,
async beforeEmit(danmu: any) {
if (danmu.direct) {
return true;
}
sendChatText(danmu.text);
return false;
}
}),
// WARN: room.currentStatus
newSyncPlugin(sendElement, room.currentStatus, () => room.currentExpireId)
@ -266,13 +279,16 @@ const handleElementMessage = (msg: ElementMessage) => {
//
case ElementMessageType.CHAT_MESSAGE: {
if (!msg.chatResp) {
return;
}
const currentTime = formatTime(new Date()); //
const senderName = msg.chatResp!.sender?.username;
const messageContent = msg.chatResp!.message;
const messageWithTime = `${senderName}${messageContent} <small>[${currentTime}]</small>`;
const senderName = msg.chatResp.sender?.username;
const messageContent = `${senderName}: ${msg.chatResp.message}`;
const messageWithTime = `${messageContent} <small>[${currentTime}]</small>`;
//
sendMsg(messageWithTime);
sendDanmuku(msg.chatResp!.message);
sendDanmuku(messageContent);
break;
}
case ElementMessageType.PLAY:
@ -380,7 +396,7 @@ onMounted(async () => {
// sessionStorage
const storedMessages = sessionStorage.getItem(`chatMessages-${roomID}`);
if (storedMessages) {
msgList.value = JSON.parse(storedMessages);
chatMsgList.value = JSON.parse(storedMessages);
}
// websocket
@ -437,7 +453,7 @@ onMounted(async () => {
<div class="card-title">在线聊天</div>
<div class="card-body mb-2">
<div class="chatArea" ref="chatArea">
<div class="message" v-for="item in msgList" :key="item">
<div class="message" v-for="item in chatMsgList" :key="item">
<div v-html="item"></div>
</div>
</div>
@ -449,13 +465,18 @@ onMounted(async () => {
>
<input
type="text"
@keyup.enter="sendText()"
v-model="sendText_"
@keyup.enter="() => sendChatText(sendChatMsg_, onSendSuccess)"
v-model="sendChatMsg_"
placeholder="按 Enter 键即可发送..."
class="l-input w-full bg-transparent"
autocomplete="off"
/>
<button class="btn w-24 m-2.5 ml-0" @click="sendText()">发送</button>
<button
class="btn w-24 m-2.5 ml-0"
@click="() => sendChatText(sendChatMsg_, onSendSuccess)"
>
发送
</button>
</div>
</div>
</el-col>