import { newClientRouter } from "../server-route"; import { sessionRouter } from "./router/session-router"; import { sessionClientStore } from "./store/client-store"; import { ClientSession } from "./type"; export const newClientSession = (arg?: { on: Partial<{ messageReceived: (session: ClientSession) => Promise; afterLogin: (session: ClientSession) => Promise; afterLogout: (session: ClientSession) => Promise; afterRecheck: (session: ClientSession) => Promise; }>; }) => { const store = sessionClientStore(); const client = newClientRouter(sessionRouter); const session: ClientSession = { status: "checking", current: null, async connect() { const url = new URL(location.href); url.protocol = "wss:"; const ws = new WebSocket(url); ws.onopen = () => { ws.send("ok"); }; ws.onmessage = (m) => { console.log(m); }; // const current = await store.load(); // if (!current) { // this.status = "guest"; // } else { // this.status = await client.check(current.uid, current.sid); // } return { status: this.status }; }, async login(arg: { method: "user-pass"; username: string; password: string; }) {}, async logout() {}, }; session.connect(); return session; };