|
|
@ -14,16 +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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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> = {};
|
|
|
|
|
|
|
|
|
|
|
@ -44,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;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -100,8 +97,9 @@ 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("/live", {
|
|
|
|
fastify.post("/live", {
|
|
|
@ -111,22 +109,21 @@ fastify.post("/live", {
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, (request, reply) => {
|
|
|
|
}, (request, reply) => {
|
|
|
|
const { code } = request.body;
|
|
|
|
const { code } = request.body;
|
|
|
|
let room, roomId;
|
|
|
|
let room, roomId;
|
|
|
|
do{
|
|
|
|
do {
|
|
|
|
roomId = generateId();
|
|
|
|
roomId = generateId();
|
|
|
|
room = rooms[roomId];
|
|
|
|
room = rooms[roomId];
|
|
|
|
} while(room);
|
|
|
|
} while (room);
|
|
|
|
|
|
|
|
|
|
|
|
room = {
|
|
|
|
room = {
|
|
|
|
sockets: [],
|
|
|
|
sockets: [],
|
|
|
|
updates: [],
|
|
|
|
updates: [],
|
|
|
|
doc: Text.of([code])
|
|
|
|
doc: Text.of([code]),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
rooms[roomId] = room;
|
|
|
|
rooms[roomId] = room;
|
|
|
|
return roomId;
|
|
|
|
return roomId;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fastify.post("/run", {
|
|
|
|
fastify.post("/run", {
|
|
|
|
schema: {
|
|
|
|
schema: {
|
|
|
|