From 29736cd98d161822f1200bf7a651aa97cdeaabe1 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 11 Apr 2020 13:35:27 +0200 Subject: [PATCH] Relay messages from proxy to clients --- src/controllers/CallsController.ts | 36 +++++++++++++++++++-- src/controllers/RTCRelayController.ts | 46 +++++++++++++++++++++++---- src/helpers/EventsHelper.ts | 8 +++++ 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/controllers/CallsController.ts b/src/controllers/CallsController.ts index c5a6d94..586dc85 100644 --- a/src/controllers/CallsController.ts +++ b/src/controllers/CallsController.ts @@ -1,7 +1,7 @@ import { RequestHandler } from "../entities/RequestHandler"; import { UserWebSocketRequestsHandler } from "../entities/WebSocketRequestHandler"; import { ActiveClient, UserWebSocketController } from "./UserWebSocketController"; -import { EventsHelper } from "../helpers/EventsHelper"; +import { EventsHelper, CallSignalFromRTCRelayEvent } from "../helpers/EventsHelper"; import * as ws from 'ws' import { WsMessage } from "../entities/WsMessage"; import { conf } from "../helpers/ConfigHelper"; @@ -112,7 +112,7 @@ export class CallsController { */ public static async OnClientSignal(h: UserWebSocketRequestsHandler) { const callID = h.postCallId("callID"); - const peerID = h.postCallPeerID(callID, "peerID"); + const peerID = h.postCallPeerID(callID, "peerID"); // The ID of the user we stream the audio / video from const type = h.postString("type"); const data = h.postJSON("data"); @@ -138,6 +138,33 @@ export class CallsController { h.success() } + /** + * Handles proxy signals + * + * @param e Event information + */ + public static async OnProxySignal(e: CallSignalFromRTCRelayEvent) { + + // Extract information + const callID = Number(e.callHash.split("-")[0]) + const peerID = Number(e.callHash.split("-")[1]) + + let targetUser = Number(e.peerID) + if(targetUser == 0) + targetUser = peerID; + + // Send the message + await UserWebSocketController.SendToSpecifcClients( + (c) => c.userID == targetUser && c.activeCalls.has(callID), + () => WsMessage.NoIDMessage("new_call_signal", { + callID: callID, + peerID: peerID, + data: e.data + }) + ) + + } + /** * Make the client leave the call * @@ -167,6 +194,11 @@ EventsHelper.Listen("user_ws_closed", async w => { await CallsController.MakeUserLeaveCall(convID, w.client) }); +// Listen to signal from RTC proxy +EventsHelper.Listen("rtc_relay_signal", async msg => { + await CallsController.OnProxySignal(msg) +}) + // Close all call when RTC WS is closed EventsHelper.Listen("rtc_relay_ws_closed", async () => { for(const client of UserWebSocketController.active_clients) { diff --git a/src/controllers/RTCRelayController.ts b/src/controllers/RTCRelayController.ts index c43b97b..c0722d7 100644 --- a/src/controllers/RTCRelayController.ts +++ b/src/controllers/RTCRelayController.ts @@ -64,6 +64,8 @@ export class RTCRelayController { // Register to events ws.addEventListener("close", () => this.WSClosed()); + ws.addEventListener("error", (e) => console.error("RTC WS error !", e)) + ws.addEventListener("message", (msg) => this.OnMessage(msg.data)) // Send ice configuration to server this.SendMessage({ @@ -103,11 +105,43 @@ export class RTCRelayController { * @param content The content of the message */ public static async SendMessage(msg: RTCSocketMessage) { - this.currWs.send(JSON.stringify({ - title: msg.title, - callHash: msg.callHash, - peerId: msg.peerId, - data: msg.data - })); + + if(this.currWs) + this.currWs.send(JSON.stringify({ + title: msg.title, + callHash: msg.callHash, + peerId: msg.peerId, + data: msg.data + })); + } + + /** + * Handles new messages data + * + * @param data Message data + */ + private static async OnMessage(data: any) { + try { + const message: RTCSocketMessage = JSON.parse(data); + + switch(message.title){ + + case "signal": + await EventsHelper.Emit("rtc_relay_signal", { + peerID: message.peerId, + callHash: message.callHash, + data: message.data + }) + break; + + default: + console.error("Unkown message type: " + message.title) + break; + } + + + } catch(e) { + console.error("RTC WS message error", e) + } } } \ No newline at end of file diff --git a/src/helpers/EventsHelper.ts b/src/helpers/EventsHelper.ts index ad36e81..c59f954 100644 --- a/src/helpers/EventsHelper.ts +++ b/src/helpers/EventsHelper.ts @@ -12,6 +12,13 @@ import { ActiveClient } from "../controllers/UserWebSocketController"; // When RTC Relay WebSocket is closed export interface ClosedRelayWebSocketEvent {} +// When a signal is sent from the RTC proxy +export interface CallSignalFromRTCRelayEvent { + peerID: string, + callHash: string, + data: any +} + // When a user sign out export interface DestroyedLoginTokensEvent { userID: number, @@ -68,6 +75,7 @@ export interface CommentDeletedEvent { */ export interface EventsMap { "rtc_relay_ws_closed": ClosedRelayWebSocketEvent, + "rtc_relay_signal": CallSignalFromRTCRelayEvent, "destroyed_login_tokens": DestroyedLoginTokensEvent, "user_ws_closed": UserWebSocketClosedEvent, "updated_number_notifications": UpdatedNotificationsNumberEvent,