add hub get logic and part of client

This commit is contained in:
2026-04-28 21:22:33 +02:00
parent a49f9f4615
commit 221fb47495
24 changed files with 1788 additions and 22 deletions
+26
View File
@@ -0,0 +1,26 @@
let socket = null
const handlers = {}
export function wsOnEvent(type, fn) {
handlers[type] = fn
}
export function wsConnect() {
if (socket) return
const token = localStorage.getItem('token') || ''
socket = new WebSocket('ws://localhost:8080/ws')
socket.onopen = () => socket.send(JSON.stringify({ token }))
socket.onmessage = (e) => {
try {
const msg = JSON.parse(e.data)
handlers[msg.type]?.(msg.event)
} catch (err) { console.error('[ws] message error', err) }
}
socket.onclose = () => { socket = null }
socket.onerror = (err) => console.error('[ws] error', err)
}
export function wsDisconnect() {
socket?.close()
socket = null
}