file switching logic

This commit is contained in:
Ishaan Dey
2024-04-27 00:20:17 -04:00
parent b348f1d519
commit 39696128db
4 changed files with 61 additions and 31 deletions

View File

@ -28,9 +28,6 @@ const handshakeSchema = z.object({
io.use(async (socket, next) => {
const q = socket.handshake.query
console.log("middleware")
const parseQuery = handshakeSchema.safeParse(q)
if (!parseQuery.success) {
@ -40,12 +37,9 @@ io.use(async (socket, next) => {
}
const { sandboxId, userId } = parseQuery.data
const dbUser = await fetch(`http://localhost:8787/api/user?id=${userId}`)
const dbUserJSON = (await dbUser.json()) as User
console.log("dbUserJSON:", dbUserJSON)
if (!dbUserJSON) {
console.log("DB error.")
next(new Error("DB error."))
@ -77,6 +71,13 @@ io.on("connection", async (socket) => {
const sandboxFiles = await getSandboxFiles(data.id)
socket.emit("loaded", sandboxFiles.files)
socket.on("getFile", (fileId: string, callback) => {
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
// console.log("file " + file.id + ": ", file.data)
callback(file.data)
})
})
httpServer.listen(port, () => {