Make calls automatically disconnect if the widget disappears (#9862)

If something goes wrong with the widget, there's no way we can continue the call, so this acts as a safeguard against stuck calls in cases where the widget crashes or disconnects weirdly.
This commit is contained in:
Robin 2023-01-04 15:51:42 -05:00 committed by GitHub
parent 432ce3ca31
commit 720bf0573a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View file

@ -255,6 +255,7 @@ export abstract class Call extends TypedEventEmitter<CallEvent, CallEventHandler
} }
this.room.on(RoomEvent.MyMembership, this.onMyMembership); this.room.on(RoomEvent.MyMembership, this.onMyMembership);
WidgetMessagingStore.instance.on(WidgetMessagingStoreEvent.StopMessaging, this.onStopMessaging);
window.addEventListener("beforeunload", this.beforeUnload); window.addEventListener("beforeunload", this.beforeUnload);
this.connectionState = ConnectionState.Connected; this.connectionState = ConnectionState.Connected;
} }
@ -275,6 +276,7 @@ export abstract class Call extends TypedEventEmitter<CallEvent, CallEventHandler
*/ */
public setDisconnected() { public setDisconnected() {
this.room.off(RoomEvent.MyMembership, this.onMyMembership); this.room.off(RoomEvent.MyMembership, this.onMyMembership);
WidgetMessagingStore.instance.off(WidgetMessagingStoreEvent.StopMessaging, this.onStopMessaging);
window.removeEventListener("beforeunload", this.beforeUnload); window.removeEventListener("beforeunload", this.beforeUnload);
this.messaging = null; this.messaging = null;
this.connectionState = ConnectionState.Disconnected; this.connectionState = ConnectionState.Disconnected;
@ -292,6 +294,13 @@ export abstract class Call extends TypedEventEmitter<CallEvent, CallEventHandler
if (membership !== "join") this.setDisconnected(); if (membership !== "join") this.setDisconnected();
}; };
private onStopMessaging = (uid: string) => {
if (uid === this.widgetUid) {
logger.log("The widget died; treating this as a user hangup");
this.setDisconnected();
}
};
private beforeUnload = () => this.setDisconnected(); private beforeUnload = () => this.setDisconnected();
} }

View file

@ -784,6 +784,13 @@ describe("ElementCall", () => {
expect(call.connectionState).toBe(ConnectionState.Connected); expect(call.connectionState).toBe(ConnectionState.Connected);
}); });
it("disconnects if the widget dies", async () => {
await call.connect();
expect(call.connectionState).toBe(ConnectionState.Connected);
WidgetMessagingStore.instance.stopMessaging(widget, room.roomId);
expect(call.connectionState).toBe(ConnectionState.Disconnected);
});
it("tracks participants in room state", async () => { it("tracks participants in room state", async () => {
expect(call.participants).toEqual(new Map()); expect(call.participants).toEqual(new Map());