2020-12-05 20:00:53 +05:30
|
|
|
import {
|
|
|
|
encryptAESGEM,
|
|
|
|
SocketUpdateData,
|
|
|
|
SocketUpdateDataSource,
|
|
|
|
} from "../data";
|
|
|
|
|
|
|
|
import CollabWrapper from "./CollabWrapper";
|
2020-04-12 16:24:52 +05:30
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
import { ExcalidrawElement } from "../../element/types";
|
|
|
|
import { BROADCAST, SCENE } from "../app_constants";
|
2021-03-28 19:26:03 +05:30
|
|
|
import { UserIdleState } from "../../types";
|
2021-03-10 17:45:37 +02:00
|
|
|
import { trackEvent } from "../../analytics";
|
2020-04-12 16:24:52 +05:30
|
|
|
|
|
|
|
class Portal {
|
2021-01-25 10:47:35 +01:00
|
|
|
collab: CollabWrapper;
|
2020-04-12 16:24:52 +05:30
|
|
|
socket: SocketIOClient.Socket | null = null;
|
|
|
|
socketInitialized: boolean = false; // we don't want the socket to emit any updates until it is fully initialized
|
2020-11-29 18:32:51 +02:00
|
|
|
roomId: string | null = null;
|
2020-04-12 16:24:52 +05:30
|
|
|
roomKey: string | null = null;
|
2020-10-21 19:11:20 +05:30
|
|
|
broadcastedElementVersions: Map<string, number> = new Map();
|
2020-04-12 16:24:52 +05:30
|
|
|
|
2021-01-25 10:47:35 +01:00
|
|
|
constructor(collab: CollabWrapper) {
|
|
|
|
this.collab = collab;
|
2020-04-27 10:56:08 -07:00
|
|
|
}
|
|
|
|
|
2020-04-12 16:24:52 +05:30
|
|
|
open(socket: SocketIOClient.Socket, id: string, key: string) {
|
|
|
|
this.socket = socket;
|
2020-11-29 18:32:51 +02:00
|
|
|
this.roomId = id;
|
2020-04-12 16:24:52 +05:30
|
|
|
this.roomKey = key;
|
2020-04-27 10:56:08 -07:00
|
|
|
|
2021-01-25 10:47:35 +01:00
|
|
|
// Initialize socket listeners
|
2020-04-28 09:49:00 -07:00
|
|
|
this.socket.on("init-room", () => {
|
2020-04-27 10:56:08 -07:00
|
|
|
if (this.socket) {
|
2020-11-29 18:32:51 +02:00
|
|
|
this.socket.emit("join-room", this.roomId);
|
2021-03-10 17:45:37 +02:00
|
|
|
trackEvent("share", "room joined");
|
2020-04-27 10:56:08 -07:00
|
|
|
}
|
|
|
|
});
|
2020-08-14 20:14:22 +02:00
|
|
|
this.socket.on("new-user", async (_socketId: string) => {
|
2020-12-05 20:00:53 +05:30
|
|
|
this.broadcastScene(
|
|
|
|
SCENE.INIT,
|
2021-04-21 23:37:44 +02:00
|
|
|
this.collab.getSyncableElements(
|
|
|
|
this.collab.getSceneElementsIncludingDeleted(),
|
|
|
|
),
|
2020-12-05 20:00:53 +05:30
|
|
|
/* syncAll */ true,
|
|
|
|
);
|
2020-04-27 10:56:08 -07:00
|
|
|
});
|
2020-04-28 09:49:00 -07:00
|
|
|
this.socket.on("room-user-change", (clients: string[]) => {
|
2021-01-25 10:47:35 +01:00
|
|
|
this.collab.setCollaborators(clients);
|
2020-04-28 09:49:00 -07:00
|
|
|
});
|
2020-04-12 16:24:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
if (!this.socket) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.socket.close();
|
|
|
|
this.socket = null;
|
2020-11-29 18:32:51 +02:00
|
|
|
this.roomId = null;
|
2020-04-12 16:24:52 +05:30
|
|
|
this.roomKey = null;
|
2020-11-09 15:34:26 +01:00
|
|
|
this.socketInitialized = false;
|
|
|
|
this.broadcastedElementVersions = new Map();
|
2020-04-12 16:24:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
isOpen() {
|
|
|
|
return !!(
|
|
|
|
this.socketInitialized &&
|
|
|
|
this.socket &&
|
2020-11-29 18:32:51 +02:00
|
|
|
this.roomId &&
|
2020-04-12 16:24:52 +05:30
|
|
|
this.roomKey
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _broadcastSocketData(
|
|
|
|
data: SocketUpdateData,
|
|
|
|
volatile: boolean = false,
|
|
|
|
) {
|
|
|
|
if (this.isOpen()) {
|
|
|
|
const json = JSON.stringify(data);
|
|
|
|
const encoded = new TextEncoder().encode(json);
|
|
|
|
const encrypted = await encryptAESGEM(encoded, this.roomKey!);
|
|
|
|
this.socket!.emit(
|
|
|
|
volatile ? BROADCAST.SERVER_VOLATILE : BROADCAST.SERVER,
|
2020-11-29 18:32:51 +02:00
|
|
|
this.roomId,
|
2020-04-12 16:24:52 +05:30
|
|
|
encrypted.data,
|
|
|
|
encrypted.iv,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 19:11:20 +05:30
|
|
|
|
|
|
|
broadcastScene = async (
|
|
|
|
sceneType: SCENE.INIT | SCENE.UPDATE,
|
2020-12-05 20:00:53 +05:30
|
|
|
syncableElements: ExcalidrawElement[],
|
2020-10-21 19:11:20 +05:30
|
|
|
syncAll: boolean,
|
|
|
|
) => {
|
|
|
|
if (sceneType === SCENE.INIT && !syncAll) {
|
|
|
|
throw new Error("syncAll must be true when sending SCENE.INIT");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!syncAll) {
|
|
|
|
// sync out only the elements we think we need to to save bandwidth.
|
|
|
|
// periodically we'll resync the whole thing to make sure no one diverges
|
|
|
|
// due to a dropped message (server goes down etc).
|
|
|
|
syncableElements = syncableElements.filter(
|
|
|
|
(syncableElement) =>
|
|
|
|
!this.broadcastedElementVersions.has(syncableElement.id) ||
|
|
|
|
syncableElement.version >
|
|
|
|
this.broadcastedElementVersions.get(syncableElement.id)!,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const data: SocketUpdateDataSource[typeof sceneType] = {
|
|
|
|
type: sceneType,
|
|
|
|
payload: {
|
|
|
|
elements: syncableElements,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const syncableElement of syncableElements) {
|
|
|
|
this.broadcastedElementVersions.set(
|
|
|
|
syncableElement.id,
|
|
|
|
syncableElement.version,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const broadcastPromise = this._broadcastSocketData(
|
|
|
|
data as SocketUpdateData,
|
|
|
|
);
|
|
|
|
|
2021-02-03 19:14:26 +01:00
|
|
|
if (syncAll && this.collab.isCollaborating) {
|
2020-10-21 19:11:20 +05:30
|
|
|
await Promise.all([
|
|
|
|
broadcastPromise,
|
2021-01-25 10:47:35 +01:00
|
|
|
this.collab.saveCollabRoomToFirebase(syncableElements),
|
2020-10-21 19:11:20 +05:30
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
await broadcastPromise;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-04 11:55:43 +01:00
|
|
|
broadcastIdleChange = (userState: UserIdleState) => {
|
|
|
|
if (this.socket?.id) {
|
|
|
|
const data: SocketUpdateDataSource["IDLE_STATUS"] = {
|
|
|
|
type: "IDLE_STATUS",
|
|
|
|
payload: {
|
|
|
|
socketId: this.socket.id,
|
|
|
|
userState,
|
|
|
|
username: this.collab.state.username,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return this._broadcastSocketData(
|
|
|
|
data as SocketUpdateData,
|
|
|
|
true, // volatile
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-21 19:11:20 +05:30
|
|
|
broadcastMouseLocation = (payload: {
|
|
|
|
pointer: SocketUpdateDataSource["MOUSE_LOCATION"]["payload"]["pointer"];
|
|
|
|
button: SocketUpdateDataSource["MOUSE_LOCATION"]["payload"]["button"];
|
|
|
|
}) => {
|
|
|
|
if (this.socket?.id) {
|
|
|
|
const data: SocketUpdateDataSource["MOUSE_LOCATION"] = {
|
|
|
|
type: "MOUSE_LOCATION",
|
|
|
|
payload: {
|
|
|
|
socketId: this.socket.id,
|
|
|
|
pointer: payload.pointer,
|
|
|
|
button: payload.button || "up",
|
2021-01-25 10:47:35 +01:00
|
|
|
selectedElementIds: this.collab.excalidrawAPI.getAppState()
|
|
|
|
.selectedElementIds,
|
|
|
|
username: this.collab.state.username,
|
2020-10-21 19:11:20 +05:30
|
|
|
},
|
|
|
|
};
|
|
|
|
return this._broadcastSocketData(
|
|
|
|
data as SocketUpdateData,
|
|
|
|
true, // volatile
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2020-04-12 16:24:52 +05:30
|
|
|
}
|
2020-10-21 19:11:20 +05:30
|
|
|
|
2020-04-12 16:24:52 +05:30
|
|
|
export default Portal;
|