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

Loading…
Cancel
Save