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(); const members = ref([]); const currentUrl = ref(""); 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)); } } })