diff --git a/commons/src/message-transporters/message-transporter.ts b/commons/src/message-transporters/message-transporter.ts index b3112c985..5b3c4d371 100644 --- a/commons/src/message-transporters/message-transporter.ts +++ b/commons/src/message-transporters/message-transporter.ts @@ -42,6 +42,14 @@ export class MessageTransporter extends EventEmitter2 { * @throws Error if no transport adapter has been set */ public sendMessage(content: Message): void { + if (this.transportAdapter === undefined) { + console.debug( + "Can't send message without transport adapter. Message that couldn't be sent was", + content + ) + return + } + if (!this.isConnected()) { this.onDisconnecting() console.debug( @@ -51,6 +59,22 @@ export class MessageTransporter extends EventEmitter2 { return } + if ( + !this.thisSideReady && + content.type !== MessageType.READY_REQUEST && + content.type !== MessageType.READY_ANSWER + ) { + throw new Error("Can't send message. This side isn't ready") + } + + if ( + !this.otherSideReady && + content.type !== MessageType.READY_REQUEST && + content.type !== MessageType.READY_ANSWER + ) { + throw new Error("Can't send message. Other side isn't ready") + } + try { this.transportAdapter.send(content) } catch (error: unknown) {