From f371414f98c896b6ee4023376e058bb16568ac18 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Mon, 17 Jun 2024 17:25:40 +0300 Subject: [PATCH] [refactor]: move rtmt to core package --- backend/src/rtmt/byte_util.ts | 14 ------- backend/src/rtmt/encode_decode_message.ts | 13 +----- backend/src/rtmt/rtmt.ts | 1 - backend/src/rtmt/rtmt_websocket.ts | 4 +- core/package.json | 5 ++- core/src/index.ts | 4 +- .../src/rtmt => core/src/rtmt/client}/rtmt.ts | 19 ++++----- .../src/rtmt/client}/rtmt_websocket.ts | 40 ++++++++++--------- core/src/rtmt/encode_decode_message.ts | 15 +++++++ core/src/rtmt/index.ts | 3 ++ core/yarn.lock | 5 +++ frontend/src/MancalaApp.tsx | 7 ++-- frontend/src/context/context.tsx | 3 +- frontend/src/rtmt/byte_util.ts | 12 ------ frontend/src/rtmt/encode_decode_message.ts | 20 ---------- mobile/src/App.tsx | 7 ++-- mobile/src/const/config.ts | 2 - mobile/src/context/context.tsx | 3 +- mobile/src/rtmt/byte_util.ts | 12 ------ mobile/src/rtmt/encode_decode_message.ts | 20 ---------- mobile/src/rtmt/rtmt.ts | 2 + 21 files changed, 74 insertions(+), 137 deletions(-) delete mode 100644 backend/src/rtmt/byte_util.ts rename {frontend/src/rtmt => core/src/rtmt/client}/rtmt.ts (62%) rename {frontend/src/rtmt => core/src/rtmt/client}/rtmt_websocket.ts (75%) create mode 100644 core/src/rtmt/encode_decode_message.ts create mode 100644 core/src/rtmt/index.ts delete mode 100644 frontend/src/rtmt/byte_util.ts delete mode 100644 frontend/src/rtmt/encode_decode_message.ts delete mode 100644 mobile/src/rtmt/byte_util.ts delete mode 100644 mobile/src/rtmt/encode_decode_message.ts diff --git a/backend/src/rtmt/byte_util.ts b/backend/src/rtmt/byte_util.ts deleted file mode 100644 index e803ada..0000000 --- a/backend/src/rtmt/byte_util.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Bytes } from "./rtmt" -var util= require('util'); - -const textEncoder = new util.TextEncoder() -const textDecoder = new util.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/backend/src/rtmt/encode_decode_message.ts b/backend/src/rtmt/encode_decode_message.ts index ba6f94b..11908db 100644 --- a/backend/src/rtmt/encode_decode_message.ts +++ b/backend/src/rtmt/encode_decode_message.ts @@ -1,16 +1,8 @@ -import { decodeText, encodeText } from "./byte_util"; -import { Bytes } from "./rtmt"; - -const headerLenght = 4 - export type Message = { channel: string, message: Object, } - -// -// channel is string, message is byte array -// + export function encode(message: Message) { return JSON.stringify({ channel : message.channel, @@ -18,9 +10,6 @@ export function encode(message: Message) { }); } -// -// return { channel : string, message : byte array} -// export function decode(bytes: string): Message { return JSON.parse(bytes); } diff --git a/backend/src/rtmt/rtmt.ts b/backend/src/rtmt/rtmt.ts index 238cad0..fe735d7 100644 --- a/backend/src/rtmt/rtmt.ts +++ b/backend/src/rtmt/rtmt.ts @@ -1,4 +1,3 @@ -export type Bytes = Buffer export type OnMessage = (clientID: string, message: Object) => any export type OnClientConnectionChange = (clientID: string, isOnline : boolean) => any diff --git a/backend/src/rtmt/rtmt_websocket.ts b/backend/src/rtmt/rtmt_websocket.ts index 213b8f3..fd3078d 100644 --- a/backend/src/rtmt/rtmt_websocket.ts +++ b/backend/src/rtmt/rtmt_websocket.ts @@ -1,10 +1,10 @@ -import { decode, encode } from "./encode_decode_message" -import { Bytes, OnClientConnectionChange, OnMessage, RTMT } from "./rtmt" +import { OnClientConnectionChange, OnMessage, RTMT } from "./rtmt" import WebSocket from "ws" import * as http from 'http'; import { channel_ping, channel_pong } from "@mancala/core"; import { RTMT_WS_PING_INTERVAL } from "../consts/config"; +import { decode, encode } from "./encode_decode_message"; export class RTMTWS implements RTMT { diff --git a/core/package.json b/core/package.json index 79aff9d..47185b0 100644 --- a/core/package.json +++ b/core/package.json @@ -15,7 +15,8 @@ "author": "Halit Aksoy", "license": "MIT", "dependencies": { - "mancala.js": "^0.0.2-beta.3" + "mancala.js": "^0.0.2-beta.3", + "tiny-emitter": "^2.1.0" }, "devDependencies": { "@types/jest": "^27.4.1", @@ -42,4 +43,4 @@ "url": "https://github.com/jhalitaksoy/mancala/issues" }, "homepage": "https://github.com/jhalitaksoy/mancala#readme" -} \ No newline at end of file +} diff --git a/core/src/index.ts b/core/src/index.ts index 2acaa4b..c53877a 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -1,2 +1,2 @@ -export * from './rtmt/channel_names' -export * from './models/index' \ No newline at end of file +export * from './models/index' +export * from './rtmt/index' \ No newline at end of file diff --git a/frontend/src/rtmt/rtmt.ts b/core/src/rtmt/client/rtmt.ts similarity index 62% rename from frontend/src/rtmt/rtmt.ts rename to core/src/rtmt/client/rtmt.ts index 3b92b2a..0407cea 100644 --- a/frontend/src/rtmt/rtmt.ts +++ b/core/src/rtmt/client/rtmt.ts @@ -1,18 +1,19 @@ -import EventEmitter2, { Listener } from "eventemitter2" +import { TinyEmitter } from "tiny-emitter" -export type Bytes = Uint8Array -export type OnMessage = (message : Object) => any +export type OnMessage = (message: Object) => any export type ConnectionState = "none" | "connecting" | "error" | "connected" | "closed" | "reconnecting"; export type RtmtEventTypes = "open" | "close" | "connected" | "error" | "disconnected" | "message" | "connectionchange"; -export interface RTMT extends EventEmitter2 { - get connectionState() : ConnectionState; +export const RTMT_WS_PING_INTERVAL = 1000, RTMT_WS_PING_INTERVAL_BUFFER_TIME = 2000; + +export interface RTMT extends TinyEmitter { + get connectionState(): ConnectionState; sendMessage: (channel: string, message: Object) => void; - addMessageListener(channel: string, callback: (message: any) => void); - removeMessageListener(channel: string, callback: (message: any) => void); - on(event: RtmtEventTypes, callback: (...value: any[]) => void): Listener | this; + addMessageListener(channel: string, callback: (message: any) => void): any; + removeMessageListener(channel: string, callback: (message: any) => void): any; + on(event: RtmtEventTypes, callback: (...value: any[]) => void): this; off(event: RtmtEventTypes, callback: (...value: any[]) => void): this; - dispose(); + dispose(): any; } \ No newline at end of file diff --git a/frontend/src/rtmt/rtmt_websocket.ts b/core/src/rtmt/client/rtmt_websocket.ts similarity index 75% rename from frontend/src/rtmt/rtmt_websocket.ts rename to core/src/rtmt/client/rtmt_websocket.ts index 52fab5e..5cbf749 100644 --- a/frontend/src/rtmt/rtmt_websocket.ts +++ b/core/src/rtmt/client/rtmt_websocket.ts @@ -1,16 +1,16 @@ -import { decode, encode } from "./encode_decode_message"; -import { channel_ping, channel_pong } from "@mancala/core"; -import { RTMT_WS_PING_INTERVAL, RTMT_WS_PING_INTERVAL_BUFFER_TIME, server } from "../const/config"; -import EventEmitter2, { Listener } from "eventemitter2"; -import { Bytes, ConnectionState, RTMT, RtmtEventTypes } from "./rtmt"; +import { channel_ping, channel_pong } from "../channel_names"; +import { decode, encode } from "../encode_decode_message"; +import { ConnectionState, RTMT, RTMT_WS_PING_INTERVAL, RTMT_WS_PING_INTERVAL_BUFFER_TIME, RtmtEventTypes } from "./rtmt"; +import { TinyEmitter } from "tiny-emitter"; const MESSAGE_CHANNEL_PREFIX = "message_channel"; -export class RTMTWS extends EventEmitter2 implements RTMT { +export class RTMTWS extends TinyEmitter implements RTMT { private webSocket?: WebSocket; private pingTimeout?: number = undefined; private _connectionState: ConnectionState = "none"; - private userKey: string; + private userKey: string = ""; + private url: string = ""; get connectionState(): ConnectionState { return this._connectionState; @@ -21,12 +21,11 @@ export class RTMTWS extends EventEmitter2 implements RTMT { this.emit("connectionchange", this._connectionState); } - private createWebSocket() { - const url = server.wsServerAdress + "?userKey=" + this.userKey; + private createWebSocket(url: string) { const webSocket = new WebSocket(url); webSocket.onopen = () => this.onWebSocketOpen(webSocket); webSocket.onclose = () => this.onWebSocketClose(webSocket); - webSocket.onmessage = (event: MessageEvent) => this.onWebSocketMessage(webSocket, event); + webSocket.onmessage = (event: any) => this.onWebSocketMessage(webSocket, event); webSocket.onerror = (error: any) => this.onWebSocketError(webSocket, error); } @@ -34,21 +33,22 @@ export class RTMTWS extends EventEmitter2 implements RTMT { if (!this.webSocket) return; this.webSocket.onopen = () => { }; this.webSocket.onclose = () => { }; - this.webSocket.onmessage = (event: MessageEvent) => { }; + this.webSocket.onmessage = (event: any) => { }; this.webSocket.onerror = (error: any) => { }; this.webSocket = undefined; } - public connectWebSocket(userKey: string) { + public connectWebSocket(url: string, userKey: string) { this.setConnectionState("connecting"); this.userKey = userKey; - this.createWebSocket(); + this.url = url; + this.createWebSocket(url); } private reconnectWebSocket() { this.setConnectionState("reconnecting"); this.disposeWebSocket(); - setTimeout(() => this.createWebSocket(), 1000); + setTimeout(() => this.createWebSocket(this.url), 1000); } protected onWebSocketOpen(webSocket: WebSocket) { @@ -59,7 +59,7 @@ export class RTMTWS extends EventEmitter2 implements RTMT { this.emit("open"); } - protected onWebSocketMessage(webSocket: WebSocket, event: MessageEvent) { + protected onWebSocketMessage(webSocket: WebSocket, event: any) { const { channel, message } = decode(event.data); this.onMessage(channel, message); } @@ -81,6 +81,7 @@ export class RTMTWS extends EventEmitter2 implements RTMT { private heartbeat() { clearTimeout(this.pingTimeout); + // @ts-ignore this.pingTimeout = setTimeout(() => { if (!this.webSocket) return; console.log("(RTMT) WebSocket self closed"); @@ -94,11 +95,11 @@ export class RTMTWS extends EventEmitter2 implements RTMT { console.error("(RTMT) WebSocket is undefined"); return; } - const data = encode(channel, message); + const data = encode({channel, message}); this.webSocket.send(data); } - private onMessage(channel: string, message: Bytes) { + private onMessage(channel: string, message: Object) { if (channel === channel_ping) { this.heartbeat(); this.sendMessage(channel_pong, {}); @@ -108,7 +109,7 @@ export class RTMTWS extends EventEmitter2 implements RTMT { this.emit(MESSAGE_CHANNEL_PREFIX + channel, message); } - public on(event: RtmtEventTypes, callback: (...value: any[]) => void): Listener | this { + public on(event: RtmtEventTypes, callback: (...value: any[]) => void): this { return super.on(event, callback); } @@ -126,6 +127,7 @@ export class RTMTWS extends EventEmitter2 implements RTMT { public dispose() { this.disposeWebSocket(); - this.removeAllListeners(); + // TODO + //this.removeAllListeners(); } } diff --git a/core/src/rtmt/encode_decode_message.ts b/core/src/rtmt/encode_decode_message.ts new file mode 100644 index 0000000..11908db --- /dev/null +++ b/core/src/rtmt/encode_decode_message.ts @@ -0,0 +1,15 @@ +export type Message = { + channel: string, + message: Object, +} + +export function encode(message: Message) { + return JSON.stringify({ + channel : message.channel, + message : message.message + }); +} + +export function decode(bytes: string): Message { + return JSON.parse(bytes); +} diff --git a/core/src/rtmt/index.ts b/core/src/rtmt/index.ts new file mode 100644 index 0000000..85c62c6 --- /dev/null +++ b/core/src/rtmt/index.ts @@ -0,0 +1,3 @@ +export * from './client/rtmt' +export * from './client/rtmt_websocket' +export * from './channel_names' \ No newline at end of file diff --git a/core/yarn.lock b/core/yarn.lock index 5944adf..5f4d77e 100644 --- a/core/yarn.lock +++ b/core/yarn.lock @@ -2842,6 +2842,11 @@ throat@^6.0.1: resolved "https://registry.npmjs.org/throat/-/throat-6.0.1.tgz" integrity sha512-8hmiGIJMDlwjg7dlJ4yKGLK8EsYqKgPWbG3b4wjJddKNwc7N7Dpn08Df4szr/sZdMVeOstrdYSsqzX6BYbcB+w== +tiny-emitter@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/tiny-emitter/-/tiny-emitter-2.1.0.tgz#1d1a56edfc51c43e863cbb5382a72330e3555423" + integrity sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q== + tmpl@1.0.5: version "1.0.5" resolved "https://registry.npmjs.org/tmpl/-/tmpl-1.0.5.tgz" diff --git a/frontend/src/MancalaApp.tsx b/frontend/src/MancalaApp.tsx index 9c69c48..221d773 100644 --- a/frontend/src/MancalaApp.tsx +++ b/frontend/src/MancalaApp.tsx @@ -10,14 +10,14 @@ import GamePage from './routes/GamePage'; import Home from './routes/Home'; import { initContext } from './context/context'; -import { RTMTWS } from './rtmt/rtmt_websocket'; +import { RTMTWS, ConnectionState } from '@mancala/core'; import { getColorByBrightness } from './util/ColorUtil'; import { Theme } from './theme/Theme'; import LobyPage from './routes/LobyPage'; import PrivacyPage from './routes/PrivacyPage'; import swal from 'sweetalert'; -import { ConnectionState } from './rtmt/rtmt'; import Util from './util/Util'; +import { server } from './const/config'; const context = initContext(); @@ -39,7 +39,8 @@ const MancalaApp: FunctionComponent = () => { const rtmt = context.rtmt as RTMTWS; rtmt.on("error", onConnectionError); rtmt.on("connectionchange", onConnectionChange) - rtmt.connectWebSocket(userKey) + const url = server.wsServerAdress + "?userKey=" + userKey; + rtmt.connectWebSocket(url, userKey) return rtmt; } diff --git a/frontend/src/context/context.tsx b/frontend/src/context/context.tsx index ee35337..9c7b77a 100644 --- a/frontend/src/context/context.tsx +++ b/frontend/src/context/context.tsx @@ -1,7 +1,6 @@ import { server } from "../const/config"; import { Texts, TrTr } from "../const/texts"; -import { RTMT } from "../rtmt/rtmt"; -import { RTMTWS } from "../rtmt/rtmt_websocket"; +import { RTMT, RTMTWS } from "@mancala/core"; import { HttpServiceImpl } from "../service/HttpService"; import { GameStore, GameStoreImpl } from "../store/GameStore"; import { UserKeyStore, UserKeyStoreImpl } from "../store/KeyStore"; diff --git a/frontend/src/rtmt/byte_util.ts b/frontend/src/rtmt/byte_util.ts deleted file mode 100644 index a4e550a..0000000 --- a/frontend/src/rtmt/byte_util.ts +++ /dev/null @@ -1,12 +0,0 @@ -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/frontend/src/rtmt/encode_decode_message.ts b/frontend/src/rtmt/encode_decode_message.ts deleted file mode 100644 index 5dda9c1..0000000 --- a/frontend/src/rtmt/encode_decode_message.ts +++ /dev/null @@ -1,20 +0,0 @@ -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 : Object) { - return JSON.stringify({ - channel, - message - }) -} - -// -// return { channel : string, message : byte array} -// -export function decode(bytes : string) { - return JSON.parse(bytes); -} diff --git a/mobile/src/App.tsx b/mobile/src/App.tsx index 10d7b07..405b33b 100644 --- a/mobile/src/App.tsx +++ b/mobile/src/App.tsx @@ -7,8 +7,7 @@ import { HomeScreen } from './screens/HomeScreen'; import LobyScreen from './screens/LobyScreen'; import { Context, initContext } from './context/context'; import { GameScreen } from './screens/GameScreen'; -import { RTMTWS } from './rtmt/rtmt_websocket'; -import { ConnectionState } from './rtmt/rtmt'; +import { RTMTWS, ConnectionState } from '@mancala/core'; import { useTranslation } from 'react-i18next'; import Snackbar from 'react-native-snackbar'; import { NativeModules, I18nManager, Platform } from 'react-native' @@ -17,6 +16,7 @@ import { NativeModules, I18nManager, Platform } from 'react-native' import 'react-native-get-random-values'; import HelpScreen from './screens/HelpScreen'; import { getColorByBrightness } from './util/ColorUtil'; +import { server } from './const/config'; const Stack = createNativeStackNavigator(); @@ -57,7 +57,8 @@ function App() { const rtmt = context.rtmt as RTMTWS; rtmt.on("error", onConnectionError); rtmt.on("connectionchange", onConnectionChange) - rtmt.connectWebSocket(userKey) + const url = server.wsServerAdress + "?userKey=" + userKey; + rtmt.connectWebSocket(url, userKey) return rtmt; } const getTextByConnectionState = (context: Context, connectionState: ConnectionState): string => { diff --git a/mobile/src/const/config.ts b/mobile/src/const/config.ts index 5f37943..0402476 100644 --- a/mobile/src/const/config.ts +++ b/mobile/src/const/config.ts @@ -14,8 +14,6 @@ export const server: Server = useLocalServer ? { wsServerAdress: "wss://mancala.segin.one", }; -export const RTMT_WS_PING_INTERVAL = 1000, RTMT_WS_PING_INTERVAL_BUFFER_TIME = 2000; - export const WEB_APP_URL = "https://playmancala.app"; export const PRIVAcY_URL = "https://playmancala.app/privacy"; \ No newline at end of file diff --git a/mobile/src/context/context.tsx b/mobile/src/context/context.tsx index 3bca23e..470ee97 100644 --- a/mobile/src/context/context.tsx +++ b/mobile/src/context/context.tsx @@ -1,6 +1,5 @@ import { server } from "../const/config"; -import { RTMT } from "../rtmt/rtmt"; -import { RTMTWS } from "../rtmt/rtmt_websocket"; +import { RTMT, RTMTWS } from "@mancala/core"; import { HttpServiceImpl } from "../service/HttpService"; import { GameStore, GameStoreImpl } from "../store/GameStore"; import { UserKeyStore, UserKeyStoreImpl } from "../store/KeyStore"; diff --git a/mobile/src/rtmt/byte_util.ts b/mobile/src/rtmt/byte_util.ts deleted file mode 100644 index a4e550a..0000000 --- a/mobile/src/rtmt/byte_util.ts +++ /dev/null @@ -1,12 +0,0 @@ -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/mobile/src/rtmt/encode_decode_message.ts b/mobile/src/rtmt/encode_decode_message.ts deleted file mode 100644 index 5dda9c1..0000000 --- a/mobile/src/rtmt/encode_decode_message.ts +++ /dev/null @@ -1,20 +0,0 @@ -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 : Object) { - return JSON.stringify({ - channel, - message - }) -} - -// -// return { channel : string, message : byte array} -// -export function decode(bytes : string) { - return JSON.parse(bytes); -} diff --git a/mobile/src/rtmt/rtmt.ts b/mobile/src/rtmt/rtmt.ts index 08c02c4..a7777ca 100644 --- a/mobile/src/rtmt/rtmt.ts +++ b/mobile/src/rtmt/rtmt.ts @@ -7,6 +7,8 @@ export type ConnectionState = "none" | "connecting" | "error" | "connected" | "c export type RtmtEventTypes = "open" | "close" | "connected" | "error" | "disconnected" | "message" | "connectionchange"; +export const RTMT_WS_PING_INTERVAL = 1000, RTMT_WS_PING_INTERVAL_BUFFER_TIME = 2000; + export interface RTMT extends TinyEmitter { get connectionState(): ConnectionState; sendMessage: (channel: string, message: Object) => void;