Browse Source

move hello message handling to session manager

merge-requests/2/merge
Paul Tirk 1 year ago
parent
commit
31e55b1e3c
  1. 10
      src/backend.ts
  2. 156
      src/classes/SessionManager.ts

10
src/backend.ts

@ -8,7 +8,6 @@ import * as config from '../config.json';
import {SessionManager} from './classes/SessionManager';
import {RoomApiHandler} from './classes/RoomApiHandler';
import {SignalingMessageHelper} from './classes/SignalingMessageHelper';
import {BackendApiRequestHelper} from './classes/BackendApiRequestHelper';
import {JanusWebRtcServerHelper} from './classes/WebRtcServerHelpers/JanusWebRtcServerHelper';
@ -56,13 +55,6 @@ const webSocketServer = new WebSocket.Server({
const roomApiHandler = new RoomApiHandler();
app.use(roomApiHandler.getRouter());
const sessionManager = new SessionManager({ roomApiHandler, webRtcServerHelper });
new SignalingMessageHelper(
webSocketServer,
sessionManager,
backendApiRequestHelper,
webRtcServerHelper
);
new SessionManager({ webSocketServer, roomApiHandler, webRtcServerHelper, backendApiRequestHelper });
server.listen(process.env.BACKEND_PORT || config.port, () => { console.log('server started'); });

156
src/classes/SessionManager.ts

@ -1,4 +1,5 @@
import WebSocket from 'ws';
import {v4 as uuid} from 'uuid';
import {Session, ISessionSessionMessageEvent, ISessionUserMessageEvent, ISessionRoomJoinedEvent, ISessionRoomLeftEvent} from './Session';
import {BackendApiRequestHelper} from './BackendApiRequestHelper';
import {RoomApiHandler, RoomApiHandlerRoomListEvent, IRoomApiHandlerParticipantsListEvent, IRoomApiHandlerRoomDeletedEvent, IRoomApiHandlerRoomMessageEvent} from './RoomApiHandler';
@ -11,12 +12,18 @@ export class SessionManager {
private webRtcServerHelper?: AbstractWebRtcServerHelper;
private roomApiHandler: RoomApiHandler;
private backendApiRequestHelper: BackendApiRequestHelper;
constructor({ webRtcServerHelper, roomApiHandler }: IConstructorServices) {
private onSocketMessage?: (message: string) => void;
constructor({ webSocketServer, webRtcServerHelper, roomApiHandler, backendApiRequestHelper }: IConstructorServices) {
this.sessions = [];
this.webRtcServerHelper = webRtcServerHelper;
this.roomApiHandler = roomApiHandler;
this.backendApiRequestHelper = backendApiRequestHelper;
webSocketServer.on('connection', this.onSocketConnection.bind(this));
this.roomApiHandler.on('roomListEvent', this.onRoomListEvent.bind(this));
this.roomApiHandler.on('participantsListEvent', this.onParticipantsListEvent.bind(this));
@ -102,6 +109,109 @@ export class SessionManager {
}
}
private onSocketConnection(socket: WebSocket): void {
this.log('socket connected...');
this.onSocketMessage = (message: string): void => {
this.handleSocketMessage(socket, message);
};
socket.on('message', this.onSocketMessage);
}
private removeListenerOfSocket(socket: WebSocket): void {
if(this.onSocketMessage) socket.off('message', this.onSocketMessage);
}
private handleSocketMessage(socket: WebSocket, message: string): void {
const data: Message = JSON.parse(message);
if(data.type !== 'hello') {
this.log('no hello message received yet, ignoring...', data);
return;
}
this.handleHelloMessage(socket, data);
}
private async handleHelloMessage(socket: WebSocket, data: IHelloMessage): Promise<void> {
this.log('HELLO:', data);
if('resumeid' in data.hello) {
this.log('RESUMING SESSION...');
const session = this.getSessionByResumeId(data.hello.resumeid);
if(!session) {
this.sendErrorMessage(socket, { code: 'no_such_session' }, data.id);
return;
}
session.resumeSession(socket);
session.sendSocketMessage({
id: data.id,
type: 'hello',
hello: {
version: '1.0',
sessionid: session.getId(),
resumeid: session.getResumeId()
}
});
this.removeListenerOfSocket(socket);
return;
}
this.backendApiRequestHelper.setUrl(data.hello.auth.url);
const backendResponse = await this.backendApiRequestHelper.sendAuthRequest({
version: '1.0',
...data.hello.auth
});
const userId = backendResponse.userid;
const session = this.createSession({
socket: socket,
ncUserId: userId,
backendApiRequestHelper: this.backendApiRequestHelper,
webRtcServerHelper: this.webRtcServerHelper
});
const features = [];
if(this.webRtcServerHelper) features.push('mcu');
const response: IHelloMessageResponse = {
id: data.id,
type: 'hello',
hello: {
sessionid: session.getId(),
resumeid: session.getResumeId(),
userid: userId,
version: '1.0',
server: {
version: process.env.VERSION || 'unknown',
features: features
}
}
};
session.sendSocketMessage(response);
this.removeListenerOfSocket(socket);
}
private sendErrorMessage(socket: WebSocket, error: any, messageId?: string): void {
const errorMessageData = {
id: messageId,
type: 'error',
error: error
};
this.sendSocketMessage(socket, errorMessageData);
}
private sendSocketMessage(socket: WebSocket, data: Object): void {
socket.send(JSON.stringify(data));
}
private onRoomListEvent(data: RoomApiHandlerRoomListEvent): void {
const sessions = 'sessionIds' in data ? this.getSessionsByIds(data.sessionIds) : this.getSessionsForUserIds(data.userIds);
@ -283,8 +393,50 @@ export class SessionManager {
}
interface IConstructorServices {
webSocketServer: WebSocket.Server,
webRtcServerHelper?: AbstractWebRtcServerHelper,
roomApiHandler: RoomApiHandler
roomApiHandler: RoomApiHandler,
backendApiRequestHelper: BackendApiRequestHelper
}
interface IHelloMessage {
id: string,
type: 'hello',
hello: HelloMessageData
}
type HelloMessageData = IResumeHelloMessageData | IAuthHelloMessageData;
interface IResumeHelloMessageData {
version: '1.0',
resumeid: string
}
interface IAuthHelloMessageData {
version: '1.0',
auth: IAuthMessageData
}
interface IAuthMessageData {
url: string,
params: IAuthParams
}
interface IHelloMessageResponse {
id: string,
type: 'hello',
hello: IHelloMessageResponseData
}
interface IHelloMessageResponseData {
sessionid: string,
resumeid: string,
userid?: string,
version: '1.0',
server: {
version: string,
features: Array<string>
}
}
interface ISessionCreationData {

Loading…
Cancel
Save