feature : change rtmt arch

This commit is contained in:
Halit Aksoy 2022-04-23 12:53:08 +03:00
parent c801b24c79
commit d5bb11d6e7
5 changed files with 5522 additions and 54 deletions

View File

@ -62,37 +62,37 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
}
const listenMessages = () => {
context.rtmt.listenMessage(channel_on_game_update, (message: Bytes) => {
const newGame: Game = JSON.parse(decodeText(message))
context.rtmt.listenMessage(channel_on_game_update, (message: Object) => {
const newGame: Game = message as Game;
console.log("on game update");
console.log(newGame);
setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn, newGame.state))
})
context.rtmt.listenMessage(channel_on_game_update, (message: Bytes) => {
const newGame: Game = JSON.parse(decodeText(message))
context.rtmt.listenMessage(channel_on_game_update, (message: Object) => {
const newGame: Game = message as Game;
console.log("on game update");
console.log(newGame);
setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn, newGame.state))
})
context.rtmt.listenMessage("on_game_start", (message) => {
const newGame: Game = JSON.parse(decodeText(message))
context.rtmt.listenMessage("on_game_start", (message: Object) => {
const newGame: Game = message as Game;
console.log("on_game_start");
console.log(newGame);
setSearchingOpponent(false)
setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn, newGame.state))
})
context.rtmt.listenMessage("on_game_crashed", (message) => {
const newCrashMessage = decodeText(message)
context.rtmt.listenMessage("on_game_crashed", (message : any) => {
const newCrashMessage = message as string
console.log("on_game_crash");
console.log(newCrashMessage);
setCrashMessage(newCrashMessage)
})
context.rtmt.listenMessage(channel_on_game_user_leave, (message) => {
const userKeyWhoLeave = decodeText(message)
context.rtmt.listenMessage(channel_on_game_user_leave, (message : any) => {
const userKeyWhoLeave = message;
console.log("on_game_user_leave");
console.log(channel_on_game_user_leave);
setUserKeyWhoLeave(userKeyWhoLeave)
@ -113,16 +113,16 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
const newGameClick = () => {
resetGameState()
setSearchingOpponent(true)
context.rtmt.sendMessage("new_game", new Uint8Array())
context.rtmt.sendMessage("new_game", {})
}
const leaveGame = () => {
context.rtmt.sendMessage(channel_leave_game, new Uint8Array())
context.rtmt.sendMessage(channel_leave_game, {})
}
const onHoleSelect = (index: number, hole: Hole) => {
const gameMove: GameMove = { index: index }
context.rtmt.sendMessage(channel_game_move, encodeText(JSON.stringify(gameMove)))
context.rtmt.sendMessage(channel_game_move, gameMove)
}
const showConnectionState = connectionState != "connected"

View File

@ -5,48 +5,16 @@ const headerLenght = 4
//
// channel is string, message is byte array
//
export function encode(channel : string, message : Bytes) {
const channelLenght = channel.length
const messageLenght = message.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)=>{
view.setUint8(count, byte)
count++
export function encode(channel : string, message : Object) {
return JSON.stringify({
channel,
message
})
for (const byte of message) {
view.setUint8(count, byte)
count++
}
return buffer
}
//
// return { channel : string, message : byte array}
//
export function decode(bytes : Bytes) {
const view = new DataView(bytes);
const channelLenght = view.getUint32(0)
const channel = decodeText(
bytes.slice(headerLenght, headerLenght + channelLenght))
const message = bytes.slice(headerLenght + channelLenght)
return {
"channel": channel,
"message": message,
}
export function decode(bytes : string) {
return JSON.parse(bytes);
}

View File

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

View File

@ -37,7 +37,7 @@ export class RTMTWS implements RTMT {
})
}
sendMessage(channel: string, message: Bytes) {
sendMessage(channel: string, message: Object) {
console.log("(RTMT) Sending message to channel " + channel);
if (this.ws === undefined) {
console.log('(RTMT) ws is undefined')

5500
yarn.lock Normal file

File diff suppressed because it is too large Load Diff