feature : new rtmt arch

This commit is contained in:
Halit Aksoy 2022-04-23 12:54:56 +03:00
parent 4972b61621
commit 1a7a83a981
4 changed files with 32 additions and 61 deletions

View File

@ -36,13 +36,13 @@ const rtmt = new RTMTWS()
rtmt.initWebSocket(server, (userKey: string) => { rtmt.initWebSocket(server, (userKey: string) => {
const game = gameStore.get(userKey) const game = gameStore.get(userKey)
if (game) { if (game) {
rtmt.sendMessage(userKey, channel_on_game_update, encodeText(JSON.stringify(game))) rtmt.sendMessage(userKey, channel_on_game_update, game)
} }
}) })
const matchmaker = new MatchMaker() const matchmaker = new MatchMaker()
rtmt.listenMessage(channel_new_game, (userKey: string, message: Bytes) => { rtmt.listenMessage(channel_new_game, (userKey: string, message: Object) => {
matchmaker.find(userKey) matchmaker.find(userKey)
}) })
@ -53,20 +53,20 @@ matchmaker.onPlayersPaired = (userKey1: string, userKey2: string) => {
gameStore.set(userKey1, game) gameStore.set(userKey1, game)
gameStore.set(userKey2, game) gameStore.set(userKey2, game)
const data = encodeText(JSON.stringify(game)) rtmt.sendMessage(userKey1, channel_on_game_start, game)
rtmt.sendMessage(userKey1, channel_on_game_start, data) rtmt.sendMessage(userKey2, channel_on_game_start, game)
rtmt.sendMessage(userKey2, channel_on_game_start, data)
} }
rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => { rtmt.listenMessage(channel_game_move, (userKey: string, message: Object) => {
const gameMove: GameMove = JSON.parse(decodeText(message)) const gameMove: GameMove = message as GameMove;
console.log(gameMove);
const game = gameStore.get(userKey) const game = gameStore.get(userKey)
if (game) { if (game) {
try { try {
game.moveByIndex(gameMove.index, game.getPlayerNameByKey(userKey)) game.moveByIndex(gameMove.index, game.getPlayerNameByKey(userKey))
const data = encodeText(JSON.stringify(game)) rtmt.sendMessage(game.player1, channel_on_game_update, game)
rtmt.sendMessage(game.player1, channel_on_game_update, data) rtmt.sendMessage(game.player2, channel_on_game_update, game)
rtmt.sendMessage(game.player2, channel_on_game_update, data)
if (game.state == "ended") { if (game.state == "ended") {
gameStore.delete(game.player1) gameStore.delete(game.player1)
@ -75,9 +75,8 @@ rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
} catch (err : any) { } catch (err : any) {
console.log(err); console.log(err);
deleteGame(game) deleteGame(game)
const data = encodeText(err.toString()) rtmt.sendMessage(game.player1, channel_on_game_crashed, err)
rtmt.sendMessage(game.player1, channel_on_game_crashed, data) rtmt.sendMessage(game.player2, channel_on_game_crashed, err)
rtmt.sendMessage(game.player2, channel_on_game_crashed, data)
} }
} else { } else {
@ -85,13 +84,12 @@ rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
} }
}) })
rtmt.listenMessage(channel_leave_game, (userKey: string, message: Bytes) => { rtmt.listenMessage(channel_leave_game, (userKey: string, message: Object) => {
const game = gameStore.get(userKey) const game = gameStore.get(userKey)
if (game) { if (game) {
deleteGame(game) deleteGame(game)
const data = encodeText(userKey) rtmt.sendMessage(game.player1, channel_on_game_user_leave, userKey)
rtmt.sendMessage(game.player1, channel_on_game_user_leave, data) rtmt.sendMessage(game.player2, channel_on_game_user_leave, userKey)
rtmt.sendMessage(game.player2, channel_on_game_user_leave, data)
} }
}) })

View File

@ -5,51 +5,22 @@ const headerLenght = 4
export type Message = { export type Message = {
channel: string, channel: string,
data: Bytes, message: Object,
} }
// //
// channel is string, message is byte array // channel is string, message is byte array
// //
export function encode(message: Message) { export function encode(message: Message) {
const { channel, data } = message return JSON.stringify({
const channelLenght = channel.length channel : message.channel,
const messageLenght = data.length message : message.message
const totalLenght = headerLenght + channelLenght + messageLenght });
const buffer = new ArrayBuffer(totalLenght);
const view = new DataView(buffer);
view.setUint32(0, channelLenght);
const channelBytes = encodeText(channel)
let count = headerLenght
channelBytes.forEach((byte: any) => {
view.setUint8(count, byte)
count++
})
data.forEach((byte: any) => {
view.setUint8(count, byte)
count++
})
return buffer
} }
// //
// return { channel : string, message : byte array} // return { channel : string, message : byte array}
// //
export function decode(bytes: Bytes): Message { export function decode(bytes: string): Message {
const channelLenght = bytes.readInt32BE(0) return JSON.parse(bytes);
const channel = decodeText(
bytes.slice(headerLenght, headerLenght + channelLenght))
const message = bytes.slice(headerLenght + channelLenght)
return {
"channel": channel,
"data": message,
}
} }

View File

@ -1,7 +1,7 @@
export type Bytes = Buffer export type Bytes = Buffer
export type OnMessage = (clientID : string, message : Bytes) => any export type OnMessage = (clientID : string, message : Object) => any
export interface RTMT{ export interface RTMT{
sendMessage : (clientID : string, channel : string, message : Bytes) => any sendMessage : (clientID : string, channel : string, message : Object) => any
listenMessage : (channel : string, callback : OnMessage) => any listenMessage : (channel : string, callback : OnMessage) => any
} }

View File

@ -27,7 +27,7 @@ export class RTMTWS implements RTMT {
const regexResult = req.url.split(RegExp("\/\?userKey=")); const regexResult = req.url.split(RegExp("\/\?userKey="));
const clientID = regexResult[1] const clientID = regexResult[1]
this.clients.set(clientID, ws) this.clients.set(clientID, ws)
ws.on("message", (messageBytes: Bytes) => { ws.on("message", (messageBytes: string) => {
console.log('received: %s', messageBytes); console.log('received: %s', messageBytes);
this.onWebSocketMessage(clientID, messageBytes) this.onWebSocketMessage(clientID, messageBytes)
}) })
@ -47,11 +47,11 @@ export class RTMTWS implements RTMT {
} }
sendMessage(clientID: string, channel: string, message: Bytes) { sendMessage(clientID: string, channel: string, message: Object) {
if (this.wsServer) { if (this.wsServer) {
const client = this.clients.get(clientID) const client = this.clients.get(clientID)
if (client) { if (client) {
const data = encode({ channel: channel, data: message }) const data = encode({ channel: channel, message: message })
console.log("(RTMT) Sending message to channel " + channel); console.log("(RTMT) Sending message to channel " + channel);
client.send(data) client.send(data)
} else { } else {
@ -66,13 +66,15 @@ export class RTMTWS implements RTMT {
this.messageChannels.set(channel, callback) this.messageChannels.set(channel, callback)
} }
onWebSocketMessage(clientID: string, messageBytes: Bytes) { onWebSocketMessage(clientID: string, messageBytes: string) {
const message = decode(messageBytes) const message = decode(messageBytes)
this.onMessage(clientID, message.channel, message.data) console.log(message);
this.onMessage(clientID, message.channel, message.message)
} }
onMessage(clientID: string, channel: string, message: Bytes) { onMessage(clientID: string, channel: string, message: Object) {
const callback = this.messageChannels.get(channel) const callback = this.messageChannels.get(channel)
if (callback) { if (callback) {
callback(clientID, message) callback(clientID, message)