mancala/backend/src/rtmt/encode_decode_message.ts

27 lines
525 B
TypeScript
Raw Normal View History

2021-06-27 19:28:09 +03:00
import { decodeText, encodeText } from "./byte_util";
import { Bytes } from "./rtmt";
const headerLenght = 4
export type Message = {
2021-06-29 03:25:42 +03:00
channel: string,
2022-04-23 12:54:56 +03:00
message: Object,
2021-06-27 19:28:09 +03:00
}
2021-06-29 03:25:42 +03:00
2021-06-27 19:28:09 +03:00
//
// channel is string, message is byte array
//
2021-06-29 03:25:42 +03:00
export function encode(message: Message) {
2022-04-23 12:54:56 +03:00
return JSON.stringify({
channel : message.channel,
message : message.message
});
2021-06-27 19:28:09 +03:00
}
//
// return { channel : string, message : byte array}
//
2022-04-23 12:54:56 +03:00
export function decode(bytes: string): Message {
return JSON.parse(bytes);
2021-06-27 19:28:09 +03:00
}