|
|
|
@ -18,7 +18,7 @@ const clients: Record<string, FastifyReply> = {};
|
|
|
|
|
const generateId = () => nanoid(32);
|
|
|
|
|
|
|
|
|
|
let updates: Update[] = [];
|
|
|
|
|
let doc = Text.of([""]);
|
|
|
|
|
let doc = Text.of(["foo"]);
|
|
|
|
|
const rooms: Record<string, WebSocket[]> = {};
|
|
|
|
|
|
|
|
|
|
function send(socket: WebSocket, requestId: number, payload: unknown) {
|
|
|
|
@ -52,41 +52,46 @@ fastify.register(async function (fastify: Fastify) {
|
|
|
|
|
},
|
|
|
|
|
(socket, request) => {
|
|
|
|
|
const { roomId } = request.params;
|
|
|
|
|
const liveClients = rooms[roomId];
|
|
|
|
|
console.log(roomId);
|
|
|
|
|
let liveClients = rooms[roomId];
|
|
|
|
|
if(!liveClients){
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
liveClients = [];
|
|
|
|
|
rooms[roomId] = liveClients;
|
|
|
|
|
}
|
|
|
|
|
liveClients.push(socket);
|
|
|
|
|
console.log(rooms);
|
|
|
|
|
socket.on("message", message => {
|
|
|
|
|
const data = JSON.parse(message.toString());
|
|
|
|
|
const requestId = data._request;
|
|
|
|
|
console.log(data.type)
|
|
|
|
|
if (data.type === "pullUpdates") {
|
|
|
|
|
send(socket, requestId, updates.slice(data.version));
|
|
|
|
|
send(socket, requestId, updates.slice(data.version));
|
|
|
|
|
} else if (data.type === "pushUpdates") {
|
|
|
|
|
let received = data.updates.map((json: any) => ({
|
|
|
|
|
clientID: json.clientID,
|
|
|
|
|
changes: ChangeSet.fromJSON(json.changes),
|
|
|
|
|
}));
|
|
|
|
|
if (data.version != updates.length) {
|
|
|
|
|
received = rebaseUpdates(received, updates.slice(data.version));
|
|
|
|
|
}
|
|
|
|
|
for (let update of received) {
|
|
|
|
|
updates.push(update);
|
|
|
|
|
doc = update.changes.apply(doc);
|
|
|
|
|
}
|
|
|
|
|
send(
|
|
|
|
|
socket,
|
|
|
|
|
requestId,
|
|
|
|
|
received.map((update: any) => ({
|
|
|
|
|
clientID: update.clientID,
|
|
|
|
|
changes: update.changes.toJSON(),
|
|
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
let received = data.updates.map((json: any) => ({
|
|
|
|
|
clientID: json.clientID,
|
|
|
|
|
changes: ChangeSet.fromJSON(json.changes),
|
|
|
|
|
}));
|
|
|
|
|
console.log(received);
|
|
|
|
|
if (data.version != updates.length) {
|
|
|
|
|
received = rebaseUpdates(received, updates.slice(data.version));
|
|
|
|
|
}
|
|
|
|
|
console.log(received)
|
|
|
|
|
for (let update of received) {
|
|
|
|
|
console.log(update);
|
|
|
|
|
updates.push(update);
|
|
|
|
|
console.log(update,doc);
|
|
|
|
|
doc = update.changes.apply(doc);
|
|
|
|
|
console.log(update);
|
|
|
|
|
}
|
|
|
|
|
send(
|
|
|
|
|
socket,
|
|
|
|
|
requestId,
|
|
|
|
|
received.map((update: any) => ({
|
|
|
|
|
clientID: update.clientID,
|
|
|
|
|
changes: update.changes.toJSON(),
|
|
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
} else if (data.type == "getDocument") {
|
|
|
|
|
send(socket, requestId, { version: updates.length, doc: doc.toString() });
|
|
|
|
|
console.log("getDoc");
|
|
|
|
|
send(socket, requestId, { version: updates.length, doc: doc.toString() });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|