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

View File

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

View File

@ -1,7 +1,7 @@
export type Bytes = Uint8Array export type Bytes = Uint8Array
export type OnMessage = (message : Bytes) => any export type OnMessage = (message : Object) => any
export interface RTMT{ export interface RTMT{
sendMessage : (channel : string, message : Bytes) => any sendMessage : (channel : string, message : Object) => any
listenMessage : (channel : string, callback : OnMessage) => 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); console.log("(RTMT) Sending message to channel " + channel);
if (this.ws === undefined) { if (this.ws === undefined) {
console.log('(RTMT) ws is undefined') console.log('(RTMT) ws is undefined')

5500
yarn.lock Normal file

File diff suppressed because it is too large Load Diff