From 3bb5e5c1debdeee81b1625ed8ab31eabaf82f886 Mon Sep 17 00:00:00 2001 From: jhalitaksoy Date: Tue, 29 Jun 2021 03:28:53 +0300 Subject: [PATCH] logs added for rtmt --- src/rtmt/rtmt.ts | 2 +- src/rtmt/rtmt_websocket.ts | 73 ++++++++++++++++++-------------------- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/rtmt/rtmt.ts b/src/rtmt/rtmt.ts index 2effa53..d7f9889 100644 --- a/src/rtmt/rtmt.ts +++ b/src/rtmt/rtmt.ts @@ -1,4 +1,4 @@ -export type Bytes = UInt8Array +export type Bytes = Uint8Array export type OnMessage = (message : Bytes) => any export interface RTMT{ diff --git a/src/rtmt/rtmt_websocket.ts b/src/rtmt/rtmt_websocket.ts index 6a464f6..9f06887 100644 --- a/src/rtmt/rtmt_websocket.ts +++ b/src/rtmt/rtmt_websocket.ts @@ -1,70 +1,67 @@ import { decode, encode } from "./encode_decode_message" -import { OnMessage, RTMT } from "./rtmt" +import { Bytes, OnMessage, RTMT } from "./rtmt" import { context } from '../context'; import { wsServerAdress } from "../service/http_service"; -export class RTMTWS implements RTMT{ +export class RTMTWS implements RTMT { - private messageChannels : Map - private ws : WebSocket + private messageChannels: Map + private ws: WebSocket constructor() { this.messageChannels = new Map() } - initWebSocket(onopen : ()=>any) { - context.userKeyStore.getUserKey((userKey : string)=>{ - const url = wsServerAdress + '?userKey=' + userKey - const ws = new WebSocket(url) - ws.binaryType = "arraybuffer"; //for firefox - ws.onopen = () => { - console.log('ws has opened') - this.ws = ws - onopen() - } - ws.onclose = () => { - console.log('ws has closed') - //this.ws = undefined - } - - ws.onmessage = (event : MessageEvent)=>{ - this.onWebSocketMessage(this, event) - } - - ws.addEventListener("error", ev => { - console.log({ ws_error: ev }); - }) + initWebSocket(userKey: string, onopen: () => any) { + const url = wsServerAdress + '?userKey=' + userKey + const ws = new WebSocket(url) + ws.binaryType = "arraybuffer"; //for firefox + ws.onopen = () => { + console.log('(RTMT) ws has opened') + this.ws = ws + onopen() + } + ws.onclose = () => { + console.log('(RTMT) ws has closed') + //this.ws = undefined + } + + ws.onmessage = (event: MessageEvent) => { + this.onWebSocketMessage(this, event) + } + + ws.addEventListener("error", ev => { + console.log({ ws_error: ev }); }) } - sendMessage(channel : string, message : Int8Array) { - if(this.ws === undefined){ - console.log('ws is undefined') + sendMessage(channel: string, message: Bytes) { + console.log("(RTMT) Sending message to channel " + channel); + if (this.ws === undefined) { + console.log('(RTMT) ws is undefined') return } const data = encode(channel, message) - console.log("(RTMT) Sending message to channel " + channel); this.ws.send(data) } - listenMessage(channel : string, callback : OnMessage) { + listenMessage(channel: string, callback: OnMessage) { this.messageChannels.set(channel, callback) } - onWebSocketMessage(rtmt : RTMTWS, event : MessageEvent) { - console.log(event); - + onWebSocketMessage(rtmt: RTMTWS, event: MessageEvent) { const { channel, message } = decode(event.data) + console.log("(RTMT) " + { channel, message }); rtmt.onMessage(channel, message) } - onMessage(channel : string, message : Int8Array){ + onMessage(channel: string, message: Bytes) { const callback = this.messageChannels.get(channel) - if(callback){ + if (callback) { callback(message) - }else{ - console.log("Channel callback not found!" + channel); + } else { + console.log("(RTMT) Channel callback not found!" + channel); } } } \ No newline at end of file