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

@ -36,7 +36,6 @@ const handshakeSchema = zod_1.z.object({
});
io.use((socket, next) => __awaiter(void 0, void 0, void 0, function* () {
const q = socket.handshake.query;
console.log("middleware");
const parseQuery = handshakeSchema.safeParse(q);
if (!parseQuery.success) {
console.log("Invalid request.");
@ -46,7 +45,6 @@ io.use((socket, next) => __awaiter(void 0, void 0, void 0, function* () {
const { sandboxId, userId } = parseQuery.data;
const dbUser = yield fetch(`http://localhost:8787/api/user?id=${userId}`);
const dbUserJSON = (yield dbUser.json());
console.log("dbUserJSON:", dbUserJSON);
if (!dbUserJSON) {
console.log("DB error.");
next(new Error("DB error."));
@ -68,6 +66,13 @@ io.on("connection", (socket) => __awaiter(void 0, void 0, void 0, function* () {
const data = socket.data;
const sandboxFiles = yield (0, getSandboxFiles_1.default)(data.id);
socket.emit("loaded", sandboxFiles.files);
socket.on("getFile", (fileId, 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, () => {
console.log(`Server running on port ${port}`);

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, () => {