1
0
forked from noxious/client
This commit is contained in:
2024-05-28 21:54:05 +02:00
parent 9ef697d812
commit da728a1fc6
11 changed files with 242 additions and 85 deletions

View File

@ -1,34 +1,42 @@
import { defineStore, type StoreDefinition } from 'pinia'
import { io, Socket } from 'socket.io-client';
import {useCookies} from '@vueuse/integrations/useCookies'
import config from '@/config';
export const useSocketStore: StoreDefinition<any> = defineStore('socket', {
state: () => ({
socket: null as Socket | null,
isAuthenticated: false,
connection: null as Socket | null,
character: null as any,
}),
getters: {
getSocket: (state: any) => state.socket,
getAuthenticated: (state: any) => state.isAuthenticated,
getConnection: (state: any) => state.connection,
getCharacter: (state: any) => state.character,
},
actions: {
setupSocketConnection(username: string, password: string) {
this.socket = io(config.server_endpoint, {
query: { username, password },
transports: ['websocket']
setupSocketConnection() {
this.connection = io(config.server_endpoint, {
withCredentials: true,
transports: ['websocket'],
reconnectionAttempts: 5,
});
// Let the server know the user is logged in
this.connection.emit('login');
// When we can't reconnect, disconnect
this.connection.on('reconnect_failed', () => {
console.log("Reconnect failed")
this.disconnectSocket();
})
},
disconnectSocket() {
if (!this.socket) {
return;
}
if (!this.connection) return;
this.socket.disconnect();
this.socket = null;
this.connection.disconnect();
this.connection = null;
this.character = null;
this.isAuthenticated = false;
useCookies().remove('token');
},
}
});