Keep exiting code when creating a room (#5)
continuous-integration/drone/push Build is passing Details

Co-authored-by: bastien ollier <bastien.ollier@etu.uca.fr>
Reviewed-on: #5
Reviewed-by: Clément FRÉVILLE <clement.freville2@etu.uca.fr>
Co-authored-by: Bastien OLLIER <bastien.ollier@noreply.codefirst.iut.uca.fr>
Co-committed-by: Bastien OLLIER <bastien.ollier@noreply.codefirst.iut.uca.fr>
pull/6/head
Bastien OLLIER 11 months ago committed by Clément FRÉVILLE
parent 85e5d9257e
commit c0ac68a516

@ -14,18 +14,14 @@ await sender.bind(`tcp://127.0.0.1:5557`);
const receiver = new Pull();
await receiver.bind(`tcp://127.0.0.1:5558`);
const clients: Record<string, FastifyReply> = {};
const generateId = () => nanoid(32);
//let updates: Update[] = [];
//let doc = Text.of(["foo"]);
type Room = {
sockets: WebSocket[];
updates: Update[];
doc: Text;
}
};
const rooms: Record<string, Room> = {};
@ -46,7 +42,6 @@ await fastify.register(cors, {
});
fastify.register(websocket);
fastify.register(async function(fastify: Fastify) {
fastify.get(
"/live/:roomId",
@ -54,9 +49,9 @@ fastify.register(async function (fastify: Fastify) {
schema: {
params: Type.Object({
roomId: Type.String(),
})
}),
},
websocket: true
websocket: true,
},
(socket, request) => {
const { roomId } = request.params;
@ -65,7 +60,7 @@ fastify.register(async function (fastify: Fastify) {
room = {
sockets: [],
updates: [],
doc: Text.of([''])
doc: Text.of([""]),
};
rooms[roomId] = room;
}
@ -102,16 +97,33 @@ fastify.register(async function (fastify: Fastify) {
send(socket, requestId, { version: room.updates.length, doc: room.doc.toString() });
}
});
},
);
});
})
/* Route pour créer une room */
fastify.post(
"/live",
async (request, reply) => {
return generateId();
fastify.post("/live", {
schema: {
body: Type.Object({
code: Type.String(),
}),
},
);
}, (request, reply) => {
const { code } = request.body;
let room, roomId;
do {
roomId = generateId();
room = rooms[roomId];
} while (room);
room = {
sockets: [],
updates: [],
doc: Text.of([code]),
};
rooms[roomId] = room;
return roomId;
});
fastify.post("/run", {
schema: {

Loading…
Cancel
Save