Browse Source

export types in type definition files

merge-requests/2/merge
Paul Tirk 1 year ago
parent
commit
b32f667c6e
  1. 189
      src/classes/SignalingMessageHelper.ts
  2. 18
      src/types/BackendRequests.ts
  3. 24
      src/types/Events.ts
  4. 16
      src/types/RoomApi.ts
  5. 17
      src/types/SignalingMessages.ts

189
src/classes/SignalingMessageHelper.ts

@ -1,189 +0,0 @@
import WebSocket from 'ws';
import {SessionManager} from './SessionManager';
import {BackendApiRequestHelper, AuthParams} from './BackendApiRequestHelper';
import {Participant} from './RoomApiHandler';
export class SignalingMessageHelper {
private webRtcServerHelper?: AbstractWebRtcServerHelper;
private sessionManager: SessionManager;
private backendApiRequestHelper: BackendApiRequestHelper;
constructor(webSocketServer: WebSocket.Server, sessionManager: SessionManager, backendApiRequestHelper: BackendApiRequestHelper, webRtcServerHelper?: AbstractWebRtcServerHelper) {
this.sessionManager = sessionManager;
this.backendApiRequestHelper = backendApiRequestHelper;
this.webRtcServerHelper = webRtcServerHelper;
webSocketServer.on('connection', this.onConnection.bind(this));
webSocketServer.on('close', this.onDisconnect.bind(this));
}
private onConnection(socket: WebSocket): void {
this.log('user connected...');
socket.on('message', (message: string) => {
this.handleMessage(socket, message);
});
socket.on('close', () => {
this.log('user disconnected (client)');
});
}
private onDisconnect(): void {
this.log('user disconnected (server)');
}
private handleMessage(socket: WebSocket, message: string): void {
const data: Message = JSON.parse(message);
switch(data.type) {
case MessageType.HELLO:
this.handleHelloMessage(socket, data);
break;
default:
}
}
private async handleHelloMessage(socket: WebSocket, data: IHelloMessage): void {
this.log('HELLO:', data.hello);
if('resumeid' in data.hello) {
this.log('RESUMING SESSION...');
const session = this.sessionManager.getSessionByResumeId(data.hello.resumeid);
if(!session) return this.sendErrorMessage(socket, { code: 'no_such_session' }, data.id);
session.resumeSession(socket);
this.sendSocketMessage(socket, {
id: data.id,
type: MessageType.HELLO,
hello: {
version: '1.0',
sessionid: session.getId(),
resumeid: session.getResumeId()
}
});
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 = await this.sessionManager.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: MessageType.HELLO,
hello: {
sessionid: session.getId(),
resumeid: session.getResumeId(),
userid: userId,
version: '1.0',
server: {
features: features
}
}
};
this.sendSocketMessage(socket, response);
}
private sendSocketMessage(socket: WebSocket, data: Object): void {
socket.send(JSON.stringify(data));
}
private sendErrorMessage(socket: WebSocket, error, messageId?: string): void {
const errorMessageData = {
id: messageId,
type: 'error',
error: error
};
this.sendSocketMessage(socket, errorMessageData);
}
private log(message: string, data?: any): void {
console.log(`SIGNALING: ${message}`, data || '');
}
}
interface IMessageCommon {
id: string,
type: MessageType
}
type Message = IHelloMessage|IByeMessage;
interface IHelloMessage extends IMessageCommon {
type: MessageType.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: AuthParams
}
interface IHelloMessageResponse {
id: string,
type: MessageType.HELLO,
hello: IHelloMessageResponseData
}
interface IHelloMessageResponseData {
sessionid: string,
resumeid: string,
userid?: string,
version: '1.0',
server: {
features: Array<string>
}
}
interface IByeMessage extends IMessageCommon {
type: MessageType.BYE,
bye: {}
}
interface IByeMessageResponse {
id: string,
type: MessageType.BYE,
bye: {}
}
enum MessageType {
HELLO = 'hello',
BYE = 'bye',
ROOM = 'room',
CONTROL = 'control',
MESSAGE = 'message'
}

18
src/types/BackendRequests.ts

@ -1,14 +1,14 @@
import {IRoomApiRoomProperties} from './RoomApi';
type BackendRequest = IBackendAuthRequest | IBackendRoomRequest | IBackendPingRequest;
export type BackendRequest = IBackendAuthRequest | IBackendRoomRequest | IBackendPingRequest;
type BackendResponse = IBackendAuthResponse | IBackendRoomResponse | IBackendPingResponse | IBackendErrorResponse;
export type BackendResponse = IBackendAuthResponse | IBackendRoomResponse | IBackendPingResponse | IBackendErrorResponse;
/* Backend requests */
interface IBackendAuthRequest {
export interface IBackendAuthRequest {
type: BackendRequestType.AUTH,
auth: {
version: '1.0',
@ -19,7 +19,7 @@ interface IBackendAuthRequest {
}
}
interface IBackendRoomRequest {
export interface IBackendRoomRequest {
type: BackendRequestType.ROOM,
room: {
version: '1.0',
@ -30,7 +30,7 @@ interface IBackendRoomRequest {
}
}
interface IBackendPingRequest {
export interface IBackendPingRequest {
type: BackendRequestType.PING,
ping: {
roomid: string,
@ -38,7 +38,7 @@ interface IBackendPingRequest {
}
}
declare enum BackendRequestType {
export enum BackendRequestType {
AUTH = 'auth',
ROOM = 'room',
PING = 'ping'
@ -48,7 +48,7 @@ declare enum BackendRequestType {
/* Backend responses */
interface IBackendAuthResponse {
export interface IBackendAuthResponse {
type: BackendRequestType.AUTH,
auth: {
version: '1.0',
@ -59,7 +59,7 @@ interface IBackendAuthResponse {
}
}
interface IBackendRoomResponse {
export interface IBackendRoomResponse {
type: BackendRequestType.ROOM,
room: {
version: '1.0',
@ -69,7 +69,7 @@ interface IBackendRoomResponse {
}
}
interface IBackendPingResponse {
export interface IBackendPingResponse {
type: BackendRequestType.ROOM,
room: {
version: '1.0',

24
src/types/Events.ts

@ -5,13 +5,13 @@ interface IEvent {
event: EventData
}
type EventData = RoomEventData | RoomListEventData | ParticipantsListEventData;
export type EventData = RoomEventData | RoomListEventData | ParticipantsListEventData;
/* Room event */
type RoomEventData = IRoomEventDataCommon & ( IRoomJoinEventData | IRoomLeaveEventData | IRoomChangeEventData | IRoomMessageEventData );
export type RoomEventData = IRoomEventDataCommon & ( IRoomJoinEventData | IRoomLeaveEventData | IRoomChangeEventData | IRoomMessageEventData );
interface IRoomEventDataCommon {
target: 'room'
@ -48,7 +48,7 @@ interface IRoomMessageEventData {
/* Room list event */
type RoomListEventData = IRoomListEventDataCommon & ( IRoomListInviteEventData | IRoomListDisinviteEventData | IRoomListUpdateEventData );
export type RoomListEventData = IRoomListEventDataCommon & ( IRoomListInviteEventData | IRoomListDisinviteEventData | IRoomListUpdateEventData );
interface IRoomListEventDataCommon {
target: 'roomlist'
@ -81,7 +81,7 @@ interface IRoomListUpdateEventData {
/* Participants list event */
type ParticipantsListEventData = IParticipantsListEventDataCommon & ( IParticipantsListUpdateEventData );
export type ParticipantsListEventData = IParticipantsListEventDataCommon & ( IParticipantsListUpdateEventData );
interface IParticipantsListEventDataCommon {
target: 'participants'
@ -99,7 +99,7 @@ interface IParticipantsListUpdateEventData {
/* Messages */
interface IClientMessage {
export interface IClientMessage {
id: string,
type: 'message',
message: {
@ -108,7 +108,7 @@ interface IClientMessage {
}
}
interface IServerMessage {
export interface IServerMessage {
type: 'message',
message: {
sender: IMessageSender,
@ -116,9 +116,9 @@ interface IServerMessage {
}
}
type MessageRecipient = IMessageSessionRecipient | IMessageUserRecipient | IMessageRoomRecipient;
export type MessageRecipient = IMessageSessionRecipient | IMessageUserRecipient | IMessageRoomRecipient;
interface IMessageSessionRecipient {
export interface IMessageSessionRecipient {
type: MessageType.SESSION,
sessionid: string
}
@ -142,24 +142,24 @@ interface IMessageSender {
/* Enums */
declare enum RoomEventType {
export enum RoomEventType {
JOIN = 'join',
LEAVE = 'leave',
CHANGE = 'change',
MESSAGE = 'message'
}
declare enum RoomListEventType {
export enum RoomListEventType {
INVITE = 'invite',
DISINVITE = 'disinvite',
UPDATE = 'update'
}
declare enum ParticipantsListEventType {
export enum ParticipantsListEventType {
UPDATE = 'update'
}
declare enum MessageType {
export enum MessageType {
SESSION = 'session',
USER = 'user',
ROOM = 'room'

16
src/types/RoomApi.ts

@ -8,7 +8,7 @@ export enum RoomApiRequestType {
MESSAGE = 'message'
}
type RoomApiRequest = IRoomApiInviteRequest |
export type RoomApiRequest = IRoomApiInviteRequest |
IRoomApiDisinviteRequest |
IRoomApiUpdateRequest |
IRoomApiDeleteRequest |
@ -53,13 +53,13 @@ interface IRoomApiMessageRequest {
interface IRoomApiInviteRequestData {
export interface IRoomApiInviteRequestData {
userids: Array<string>,
alluserids: Array<string>,
properties: IRoomApiRoomProperties
}
type RoomApiDisinviteRequestData = RoomApiDisinviteUserIdsRequestData | RoomApiDisinviteSessionIdsRequestData;
export type RoomApiDisinviteRequestData = RoomApiDisinviteUserIdsRequestData | RoomApiDisinviteSessionIdsRequestData;
interface IRoomApiDisinviteRequestDataCommon {
alluserids: Array<string>,
@ -74,27 +74,27 @@ type RoomApiDisinviteSessionIdsRequestData = IRoomApiDisinviteRequestDataCommon
sessionids: Array<string>
};
interface IRoomApiUpdateRequestData {
export interface IRoomApiUpdateRequestData {
userids: Array<string>,
properties: IRoomApiRoomProperties
}
interface IRoomApiDeleteRequestData {
export interface IRoomApiDeleteRequestData {
userids: Array<string>
}
interface IRoomApiParticipantsRequestData {
export interface IRoomApiParticipantsRequestData {
changed: Array<IParticipant>,
users: Array<IParticipant>
}
interface IRoomApiIncallRequestData {
export interface IRoomApiIncallRequestData {
incall: number, // TODO: in call flags
changed: Array<IParticipant>,
users: Array<IParticipant>
}
interface IRoomApiMessageRequestData {
export interface IRoomApiMessageRequestData {
data: any // TODO { chat: { refresh: true } }
}

17
src/types/SignalingMessages.ts

@ -0,0 +1,17 @@
export enum ClientMessageType {
HELLO = 'hello',
BYE = 'bye',
ROOM = 'room',
CONTROL = 'control',
MESSAGE = 'message'
}
export enum MessageMessageDataType {
REQUESTOFFER = 'requestoffer',
SENDOFFER = 'sendoffer'
}
export enum RoomType {
VIDEO = 'video',
SCREEN = 'screen'
}
Loading…
Cancel
Save