-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#17 Add Documentation + Mapping Messages
- Loading branch information
1 parent
0287939
commit 7be793d
Showing
3 changed files
with
73 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |