Skip to content

Commit

Permalink
#17 Add Documentation + Mapping Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Peyton-McKee committed Sep 14, 2023
1 parent 0287939 commit 7be793d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
6 changes: 3 additions & 3 deletions scylla-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express, { Request, Response } from 'express';
import { Server, Socket } from 'socket.io';
import ProxyController from './proxy/proxy-controller';
import prisma from './prisma/prisma-client';
import { createServerMessageMap } from './utils/message-maps.utils';

const app = express();
const port = 8000;
Expand Down Expand Up @@ -31,10 +32,9 @@ const serverSocket = new Server(server, {
}
});

const serverProxyController = new ProxyController();

serverSocket.on('connection', (socket: Socket) => {
serverProxyController.handleClientConnection(socket);
const serverProxy = new ProxyController(createServerMessageMap(), socket);
serverProxy.configure();
});

//TODO: Get host/port from DNC
Expand Down
51 changes: 47 additions & 4 deletions scylla-server/src/proxy/proxy-controller.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,64 @@
import { Socket } from 'socket.io';

/**
* Proxy Controller To Handle Inputting and Outputting Messages to a Client
*/
export default class ProxyController {
messageMap: Map<string, (data: JSON) => void>;
socket: Socket;

/**
* Constructor
* @param messageMap A map of input arguments to functions that handle the input
* @param socket the socket to send and receive messages from
*/
constructor(messageMap: Map<string, (data: JSON) => void>, socket: Socket) {
this.messageMap = messageMap;
this.socket = socket;
}

/**
* Handles a message from the client
* @param data The data received from the client
*/
private handleClientMessage(data: any): void {
try {
data = JSON.parse(data);
const responseFunction = this.messageMap.get(data.argument);
if (responseFunction) {
const responseData = responseFunction(data);
this.socket.emit('message', responseData);
} else {
throw new Error('Invalid Argument');
}
} catch (error) {
console.log('error parsing message', error);
if (error instanceof Error) {
this.socket.emit('Error', error.message);
}
console.log('error in message', error);
}
}

/**
* Handles a client disconnect
*/
private handleClientDisconnect(): void {
console.log('disconnected from client');
}

public handleClientConnection(socket: Socket): void {
/**
* Handles a client connection
*/
private handleClientConnection(): void {
console.log('connected to client');
socket.on('message', this.handleClientMessage);
socket.on('disconnect', this.handleClientDisconnect);
this.socket.on('message', this.handleClientMessage);
this.socket.on('disconnect', this.handleClientDisconnect);
}

/**
* Configures the proxy controller
*/
public configure(): void {
this.handleClientConnection();
}
}
23 changes: 23 additions & 0 deletions scylla-server/src/utils/message-maps.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Creates a map of server messages to functions that handle the messages
* @returns A map of server messages to functions that handle the messages
*/
export const createServerMessageMap = (): Map<string, (data: JSON) => void> => {
const serverMessageMap = new Map<string, (data: JSON) => void>();
serverMessageMap.set('test', (data: JSON) => {
console.log('test', data);
});
return serverMessageMap;
};

/**
* Creates a map of client messages to functions that handle the messages
* @returns A map of client messages to functions that handle the messages
*/
export const createClientMessageMap = (): Map<string, (data: JSON) => void> => {
const clientMessageMap = new Map<string, (data: JSON) => void>();
clientMessageMap.set('test', (data: JSON) => {
console.log('test', data);
});
return clientMessageMap;
};

0 comments on commit 7be793d

Please sign in to comment.