diff --git a/src/App.tsx b/src/App.tsx
index 8a52dbf..a89acdd 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,6 +1,9 @@
import * as React from 'react';
import { render } from 'react-dom';
+import { initContext } from './context';
-import Counter from './Counter';
+import Home from './Home';
-render(
Clicks: {clicks}
+ + + > +} + +export default Home \ No newline at end of file diff --git a/src/context.tsx b/src/context.tsx new file mode 100644 index 0000000..481fc26 --- /dev/null +++ b/src/context.tsx @@ -0,0 +1,19 @@ +import { RTMT } from "./rtmt/rtmt" +import { RTMTWS } from "./rtmt/rtmt_websocket" +import { UserKeyStore, UserKeyStoreImpl } from "./store/key_store" + +type Context = { + rtmt : RTMT + userKeyStore : UserKeyStore +} + +export const initContext = ()=> { + const rtmt = new RTMTWS() + const userKeyStore = new UserKeyStoreImpl() + return { + rtmt : rtmt, + userKeyStore : userKeyStore, + } +} + +export const context : Context = initContext() diff --git a/src/rtmt/byte_util.ts b/src/rtmt/byte_util.ts new file mode 100644 index 0000000..a4e550a --- /dev/null +++ b/src/rtmt/byte_util.ts @@ -0,0 +1,12 @@ +import { Bytes } from "./rtmt" +const textEncoder = new TextEncoder() +const textDecoder = new TextDecoder("utf-8") + +export function encodeText(text : string) { + const bytes = textEncoder.encode(text) + return bytes +} + +export function decodeText(bytes : Bytes) { + return textDecoder.decode(bytes) +} \ No newline at end of file diff --git a/src/rtmt/encode_decode_message.ts b/src/rtmt/encode_decode_message.ts new file mode 100644 index 0000000..686c544 --- /dev/null +++ b/src/rtmt/encode_decode_message.ts @@ -0,0 +1,52 @@ +import { decodeText, encodeText } from "./byte_util"; +import { Bytes } from "./rtmt"; + +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++ + }) + + 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, + } +} diff --git a/src/rtmt/rtmt.ts b/src/rtmt/rtmt.ts new file mode 100644 index 0000000..2effa53 --- /dev/null +++ b/src/rtmt/rtmt.ts @@ -0,0 +1,7 @@ +export type Bytes = UInt8Array +export type OnMessage = (message : Bytes) => any + +export interface RTMT{ + sendMessage : (channel : string, message : Bytes) => any + listenMessage : (channel : string, callback : OnMessage) => any +} \ No newline at end of file diff --git a/src/rtmt/rtmt_websocket.ts b/src/rtmt/rtmt_websocket.ts new file mode 100644 index 0000000..6a464f6 --- /dev/null +++ b/src/rtmt/rtmt_websocket.ts @@ -0,0 +1,70 @@ +import { decode, encode } from "./encode_decode_message" +import { OnMessage, RTMT } from "./rtmt" +import { context } from '../context'; +import { wsServerAdress } from "../service/http_service"; + +export class RTMTWS implements RTMT{ + + private messageChannels : Map