haha keyboard go brrr
This commit is contained in:
@ -1,70 +1,41 @@
|
||||
import { defineStore, type StoreDefinition } from 'pinia'
|
||||
import { io, Socket } from 'socket.io-client';
|
||||
import config from '@/config';
|
||||
|
||||
export const useSocketStore: StoreDefinition<any> = defineStore('socket', {
|
||||
state: () => ({
|
||||
socket: null as Socket | null,
|
||||
isAuthenticated: false,
|
||||
character: null as any,
|
||||
}),
|
||||
getters: {
|
||||
getSocket: (state: any) => state.socket,
|
||||
getAuthenticated: (state: any) => state.isAuthenticated,
|
||||
getCharacter: (state: any) => state.character,
|
||||
},
|
||||
actions: {
|
||||
setupSocketConnection(username: string, password: string) {
|
||||
this.socket = io(config.server_endpoint, {
|
||||
query: { username, password },
|
||||
transports: ['websocket']
|
||||
});
|
||||
},
|
||||
disconnectSocket() {
|
||||
if (!this.socket) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.socket.disconnect();
|
||||
this.socket = null;
|
||||
this.character = null;
|
||||
this.isAuthenticated = false;
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Resources:
|
||||
* https://www.dynetisgames.com/2017/03/06/how-to-make-a-multiplayer-online-game-with-phaser-socket-io-and-node-js/
|
||||
* https://runthatline.com/pinia-watch-state-getters-inside-vue-components/
|
||||
* https://pinia.vuejs.org/
|
||||
*/
|
||||
import { defineStore, type StoreDefinition } from 'pinia'
|
||||
import { io, Socket } from 'socket.io-client';
|
||||
import config from '@/config';
|
||||
import axios from 'axios';
|
||||
|
||||
export const useSocketStore: StoreDefinition<any> = defineStore('socket', {
|
||||
state: () => ({
|
||||
isAuthenticated: false,
|
||||
socket: null as Socket | null,
|
||||
}),
|
||||
getters: {
|
||||
auth: (state: any) => state.isAuthenticated,
|
||||
},
|
||||
actions: {
|
||||
async register(username: string, password: string) {
|
||||
if ( this.isAuthenticated ) return console.log('User already authenticated');
|
||||
|
||||
try {
|
||||
const response = await axios.post(`${config.server_endpoint}/register`, { username, password });
|
||||
console.log(response.data);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error registering user:', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
async login(username: string, password: string) {
|
||||
if ( this.isAuthenticated ) return console.log('User already authenticated');
|
||||
|
||||
try {
|
||||
const response = await axios.post(`${config.server_endpoint}/login`, { username, password });
|
||||
if (response.status === 200) {
|
||||
this.isAuthenticated = true;
|
||||
this.setupSocketConnection(username, password);
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Login failed:', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
setupSocketConnection(username: string, password: string) {
|
||||
this.socket = io(config.server_endpoint as string, {
|
||||
query: { username, password },
|
||||
transports: ['websocket']
|
||||
});
|
||||
|
||||
this.socket.on('message', (message) => {
|
||||
console.log('Received message:', message);
|
||||
});
|
||||
// Handle more socket events as needed
|
||||
},
|
||||
|
||||
disconnectSocket() {
|
||||
if (this.socket) {
|
||||
this.socket.disconnect();
|
||||
this.socket = null;
|
||||
this.isAuthenticated = false;
|
||||
}
|
||||
},
|
||||
}
|
||||
});
|
||||
*/
|
Reference in New Issue
Block a user