update merge
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

pull/5/head
Bastien OLLIER 11 months ago committed by clfreville2
parent 150376a2da
commit fc268e93e7

@ -14,16 +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);
type Room = {
sockets: WebSocket[];
updates: Update[];
doc: Text;
}
};
const rooms: Record<string, Room> = {};
@ -44,26 +42,25 @@ await fastify.register(cors, {
});
fastify.register(websocket);
fastify.register(async function (fastify: Fastify) {
fastify.register(async function(fastify: Fastify) {
fastify.get(
"/live/:roomId",
{
schema: {
params: Type.Object({
roomId: Type.String(),
})
}),
},
websocket: true
websocket: true,
},
(socket, request) => {
const { roomId } = request.params;
let room = rooms[roomId];
if(!room){
if (!room) {
room = {
sockets: [],
updates: [],
doc: Text.of([''])
doc: Text.of([""]),
};
rooms[roomId] = room;
}
@ -100,8 +97,9 @@ 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", {
@ -113,20 +111,19 @@ fastify.post("/live", {
}, (request, reply) => {
const { code } = request.body;
let room, roomId;
do{
do {
roomId = generateId();
room = rooms[roomId];
} while(room);
} while (room);
room = {
sockets: [],
updates: [],
doc: Text.of([code])
doc: Text.of([code]),
};
rooms[roomId] = room;
return roomId;
},
);
});
fastify.post("/run", {
schema: {

Loading…
Cancel
Save