Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed Feb 19, 2024
1 parent 4af7682 commit 4bc3390
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
9 changes: 8 additions & 1 deletion routes/api/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ export const handler: Handlers = {
socket.send(JSON.stringify(mes));
}
}
const interval = setInterval(() => {
sendMessage({ type: "ping" });
}, 30000);
socket.onclose = () => {
removeListener();
clearInterval(interval);
};
socket.onerror = () => {
removeListener();
clearInterval(interval);
console.error("WebSocket error.");
};
socket.onmessage = (e) => {
Expand All @@ -70,6 +75,8 @@ export const handler: Handlers = {
running: t.get_running_task(),
});
});
} else if (d.type == "ping") {
sendMessage({ type: "pong" });
}
} catch (_) {
null;
Expand Down Expand Up @@ -144,7 +151,7 @@ export const handler: Handlers = {
}
}
try {
const task = t.add_export_zip_task(gid, dcfg);
const task = await t.add_export_zip_task(gid, dcfg);
return return_data(task, 201);
} catch (e) {
return return_error(500, e.message);
Expand Down
26 changes: 18 additions & 8 deletions server/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@ import type { DownloadConfig } from "../tasks/download.ts";
import type { ExportZipConfig } from "../tasks/export_zip.ts";
import type { DiscriminatedUnion } from "../utils.ts";

export type TaskServerSocketData = TaskEventData | { type: "close" } | {
type: "tasks";
tasks: Task[];
running: number[];
};
export type TaskServerSocketData =
| TaskEventData
| { type: "close" }
| {
type: "tasks";
tasks: Task[];
running: number[];
}
| { type: "ping" }
| { type: "pong" };

type EventMap = {
new_download_task: { gid: number; token: string; cfg?: DownloadConfig };
new_export_zip_task: { gid: number; cfg?: ExportZipConfig };
};

export type TaskClientSocketData = DiscriminatedUnion<"type", EventMap> | {
type: "close";
} | { type: "task_list" };
export type TaskClientSocketData =
| DiscriminatedUnion<"type", EventMap>
| {
type: "close";
}
| { type: "task_list" }
| { type: "ping" }
| { type: "pong" };

0 comments on commit 4bc3390

Please sign in to comment.