forked from noxious/client
yuhhh
This commit is contained in:
@ -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');
|
||||
},
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user