Last active
September 15, 2023 20:52
-
-
Save kenreilly/62be311b855b880224aad885dfcebf71 to your computer and use it in GitHub Desktop.
WebSocket API class
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
import { WebSocketMessage, WebSocketMessageType } from "../../common/ws-message.js" | |
export class WebSocketAPI { | |
private static command_table: {[name:string]: Function} = { | |
'ping': WebSocketAPI.ping, | |
'echo': WebSocketAPI.echo | |
} | |
public static handle(message: WebSocketMessage): Promise<WebSocketMessage> { | |
return new Promise((resolve, reject) => { | |
if (message.msg_type in WebSocketAPI.command_table) { | |
return resolve(WebSocketAPI.command_table[message.msg_type](message.data)) | |
} | |
reject('invalid command') | |
}) | |
} | |
private static ping(): WebSocketMessage { return new WebSocketMessage(WebSocketMessageType.PING, 'pong') } | |
private static echo (data: Object): WebSocketMessage { return new WebSocketMessage(WebSocketMessageType.ECHO, data) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment