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) => {
const game = gameStore.get(userKey)
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()
rtmt.listenMessage(channel_new_game, (userKey: string, message: Bytes) => {
rtmt.listenMessage(channel_new_game, (userKey: string, message: Object) => {
matchmaker.find(userKey)
})
@ -53,20 +53,20 @@ matchmaker.onPlayersPaired = (userKey1: string, userKey2: string) => {
gameStore.set(userKey1, game)
gameStore.set(userKey2, game)
const data = encodeText(JSON.stringify(game))
rtmt.sendMessage(userKey1, channel_on_game_start, data)
rtmt.sendMessage(userKey2, channel_on_game_start, data)
rtmt.sendMessage(userKey1, channel_on_game_start, game)
rtmt.sendMessage(userKey2, channel_on_game_start, game)
}
rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
const gameMove: GameMove = JSON.parse(decodeText(message))
rtmt.listenMessage(channel_game_move, (userKey: string, message: Object) => {
const gameMove: GameMove = message as GameMove;
console.log(gameMove);
const game = gameStore.get(userKey)
if (game) {
try {
game.moveByIndex(gameMove.index, game.getPlayerNameByKey(userKey))
const data = encodeText(JSON.stringify(game))
rtmt.sendMessage(game.player1, channel_on_game_update, data)
rtmt.sendMessage(game.player2, channel_on_game_update, data)
rtmt.sendMessage(game.player1, channel_on_game_update, game)
rtmt.sendMessage(game.player2, channel_on_game_update, game)
if (game.state == "ended") {
gameStore.delete(game.player1)
@ -75,9 +75,8 @@ rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
} catch (err : any) {
console.log(err);
deleteGame(game)
const data = encodeText(err.toString())
rtmt.sendMessage(game.player1, channel_on_game_crashed, data)
rtmt.sendMessage(game.player2, channel_on_game_crashed, data)
rtmt.sendMessage(game.player1, channel_on_game_crashed, err)
rtmt.sendMessage(game.player2, channel_on_game_crashed, err)
}
} 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)
if (game) {
deleteGame(game)
const data = encodeText(userKey)
rtmt.sendMessage(game.player1, channel_on_game_user_leave, data)
rtmt.sendMessage(game.player2, channel_on_game_user_leave, data)
rtmt.sendMessage(game.player1, channel_on_game_user_leave, userKey)
rtmt.sendMessage(game.player2, channel_on_game_user_leave, userKey)
}
})

View File

@ -5,51 +5,22 @@ const headerLenght = 4
export type Message = {
channel: string,
data: Bytes,
message: Object,
}
//
// channel is string, message is byte array
//
export function encode(message: Message) {
const { channel, data } = message
const channelLenght = channel.length
const messageLenght = data.length
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 JSON.stringify({
channel : message.channel,
message : message.message
});
}
//
// return { channel : string, message : byte array}
//
export function decode(bytes: Bytes): Message {
const channelLenght = bytes.readInt32BE(0)
const channel = decodeText(
bytes.slice(headerLenght, headerLenght + channelLenght))
const message = bytes.slice(headerLenght + channelLenght)
return {
"channel": channel,
"data": message,
}
export function decode(bytes: string): Message {
return JSON.parse(bytes);
}

View File

@ -1,7 +1,7 @@
export type Bytes = Buffer
export type OnMessage = (clientID : string, message : Bytes) => any
export type OnMessage = (clientID : string, message : Object) => any
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
}

View File

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