1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-25 06:49:23 +00:00

Relay messages from proxy to clients

This commit is contained in:
Pierre HUBERT 2020-04-11 13:35:27 +02:00
parent 2e6461c34a
commit 29736cd98d
3 changed files with 82 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import { RequestHandler } from "../entities/RequestHandler"; import { RequestHandler } from "../entities/RequestHandler";
import { UserWebSocketRequestsHandler } from "../entities/WebSocketRequestHandler"; import { UserWebSocketRequestsHandler } from "../entities/WebSocketRequestHandler";
import { ActiveClient, UserWebSocketController } from "./UserWebSocketController"; import { ActiveClient, UserWebSocketController } from "./UserWebSocketController";
import { EventsHelper } from "../helpers/EventsHelper"; import { EventsHelper, CallSignalFromRTCRelayEvent } from "../helpers/EventsHelper";
import * as ws from 'ws' import * as ws from 'ws'
import { WsMessage } from "../entities/WsMessage"; import { WsMessage } from "../entities/WsMessage";
import { conf } from "../helpers/ConfigHelper"; import { conf } from "../helpers/ConfigHelper";
@ -112,7 +112,7 @@ export class CallsController {
*/ */
public static async OnClientSignal(h: UserWebSocketRequestsHandler) { public static async OnClientSignal(h: UserWebSocketRequestsHandler) {
const callID = h.postCallId("callID"); 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 type = h.postString("type");
const data = h.postJSON("data"); const data = h.postJSON("data");
@ -138,6 +138,33 @@ export class CallsController {
h.success() 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 * Make the client leave the call
* *
@ -167,6 +194,11 @@ EventsHelper.Listen("user_ws_closed", async w => {
await CallsController.MakeUserLeaveCall(convID, w.client) 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 // Close all call when RTC WS is closed
EventsHelper.Listen("rtc_relay_ws_closed", async () => { EventsHelper.Listen("rtc_relay_ws_closed", async () => {
for(const client of UserWebSocketController.active_clients) { for(const client of UserWebSocketController.active_clients) {

View File

@ -64,6 +64,8 @@ export class RTCRelayController {
// Register to events // Register to events
ws.addEventListener("close", () => this.WSClosed()); 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 // Send ice configuration to server
this.SendMessage({ this.SendMessage({
@ -103,11 +105,43 @@ export class RTCRelayController {
* @param content The content of the message * @param content The content of the message
*/ */
public static async SendMessage(msg: RTCSocketMessage) { public static async SendMessage(msg: RTCSocketMessage) {
this.currWs.send(JSON.stringify({
title: msg.title, if(this.currWs)
callHash: msg.callHash, this.currWs.send(JSON.stringify({
peerId: msg.peerId, title: msg.title,
data: msg.data 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)
}
} }
} }

View File

@ -12,6 +12,13 @@ import { ActiveClient } from "../controllers/UserWebSocketController";
// When RTC Relay WebSocket is closed // When RTC Relay WebSocket is closed
export interface ClosedRelayWebSocketEvent {} 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 // When a user sign out
export interface DestroyedLoginTokensEvent { export interface DestroyedLoginTokensEvent {
userID: number, userID: number,
@ -68,6 +75,7 @@ export interface CommentDeletedEvent {
*/ */
export interface EventsMap { export interface EventsMap {
"rtc_relay_ws_closed": ClosedRelayWebSocketEvent, "rtc_relay_ws_closed": ClosedRelayWebSocketEvent,
"rtc_relay_signal": CallSignalFromRTCRelayEvent,
"destroyed_login_tokens": DestroyedLoginTokensEvent, "destroyed_login_tokens": DestroyedLoginTokensEvent,
"user_ws_closed": UserWebSocketClosedEvent, "user_ws_closed": UserWebSocketClosedEvent,
"updated_number_notifications": UpdatedNotificationsNumberEvent, "updated_number_notifications": UpdatedNotificationsNumberEvent,