Files
myplayer-vue/src/store/playroom.ts
2026-04-04 17:48:34 +08:00

94 lines
2.3 KiB
TypeScript

import { ref } from "vue";
import { defineStore } from "pinia";
import { connectWebSocket, disconnectWebSocket, sendMessage, setIsManualClose } from "@/websocket/roomSocket";
interface PlayroomState {
id: number
r_id: number;
r_name: string;
r_introduction: string;
r_avatar: string;
role: number;
}
interface member {
id: number;
u_id: string;
u_name: string;
u_avatar: string;
}
export const PlayroomStore = defineStore("PlayroomStore",
() =>{
const currentPlayroom = ref<PlayroomState>();
const members = ref<member[]>([]);
const currentUrl = ref<string>("");
const setCurrentPlayroom = (playroom: PlayroomState) => {
currentPlayroom.value = playroom;
}
const getCurrentPlayroom = () =>{
return currentPlayroom.value;
}
const getCurrentId = () =>{
return currentPlayroom.value?.r_id;
}
const setCurrentUrl = (url: string) => {
currentUrl.value = url;
}
const clearPlayroom = () => {
currentPlayroom.value = null;
currentUrl.value = "";
members.value = [];
}
const addmember = (member: member) => {
members.value.push(member);
}
const getmembers = (page: number, pageSize: number) => {
return members.value.slice((page - 1) * pageSize, page * pageSize);
}
return {
currentPlayroom,
currentUrl,
getCurrentId,
setCurrentPlayroom,
clearPlayroom,
addmember,
getmembers,
}
})
export const videoSocketStore = defineStore("videoSocketStore",{
state: () => ({
isConnected: false,
hasGotMessage: false,
id: 0
}),
actions: {
connect(id: number) {
this.id = id;
if (this.isConnected === true) return
connectWebSocket(id);
this.isConnected = true;
},
disconnect() {
setIsManualClose(true);
disconnectWebSocket();
this.isConnected = false;
},
send(message: string) {
sendMessage(JSON.stringify(message));
}
}
})