mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-13 08:12:40 +10:00
task API
The Task API allows for an easy way to create long-lived tasks that require reporting back to user with progress/logs. It will be used in the upcoming game importing.
This commit is contained in:
44
server/api/v1/task/index.get.ts
Normal file
44
server/api/v1/task/index.get.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { H3Event } from "h3";
|
||||
import session from "~/server/internal/session";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import taskHandler, { TaskMessage } from "~/server/internal/tasks";
|
||||
|
||||
export default defineWebSocketHandler({
|
||||
open(peer) {
|
||||
const dummyEvent = {
|
||||
node: {
|
||||
req: {
|
||||
headers: peer.headers,
|
||||
},
|
||||
},
|
||||
} as unknown as H3Event;
|
||||
const userId = session.getUserId(dummyEvent);
|
||||
if (!userId) {
|
||||
peer.send("unauthenticated");
|
||||
return;
|
||||
}
|
||||
const peerId = uuidv4();
|
||||
peer.ctx.id = peerId;
|
||||
|
||||
const rtMsg: TaskMessage = {
|
||||
id: "connect",
|
||||
success: true,
|
||||
progress: 0,
|
||||
error: undefined,
|
||||
log: [],
|
||||
};
|
||||
peer.send(rtMsg);
|
||||
},
|
||||
message(peer, message) {
|
||||
if (!peer.ctx.id) return;
|
||||
const text = message.text();
|
||||
if (text.startsWith("connect/")) {
|
||||
const id = text.substring("connect/".length);
|
||||
taskHandler.connect(peer.ctx.id, id, peer);
|
||||
return;
|
||||
}
|
||||
},
|
||||
close(peer, details) {
|
||||
if (!peer.ctx.id) return;
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user