logs added for rtmt

This commit is contained in:
jhalitaksoy 2021-06-29 03:28:53 +03:00
parent cfce0903e5
commit 3bb5e5c1de
2 changed files with 36 additions and 39 deletions

View File

@ -1,4 +1,4 @@
export type Bytes = UInt8Array export type Bytes = Uint8Array
export type OnMessage = (message : Bytes) => any export type OnMessage = (message : Bytes) => any
export interface RTMT{ export interface RTMT{

View File

@ -1,70 +1,67 @@
import { decode, encode } from "./encode_decode_message" import { decode, encode } from "./encode_decode_message"
import { OnMessage, RTMT } from "./rtmt" import { Bytes, OnMessage, RTMT } from "./rtmt"
import { context } from '../context'; import { context } from '../context';
import { wsServerAdress } from "../service/http_service"; import { wsServerAdress } from "../service/http_service";
export class RTMTWS implements RTMT{ export class RTMTWS implements RTMT {
private messageChannels : Map<String,OnMessage> private messageChannels: Map<String, OnMessage>
private ws : WebSocket private ws: WebSocket
constructor() { constructor() {
this.messageChannels = new Map<String, OnMessage>() this.messageChannels = new Map<String, OnMessage>()
} }
initWebSocket(onopen : ()=>any) { initWebSocket(userKey: string, onopen: () => any) {
context.userKeyStore.getUserKey((userKey : string)=>{ const url = wsServerAdress + '?userKey=' + userKey
const url = wsServerAdress + '?userKey=' + userKey const ws = new WebSocket(url)
const ws = new WebSocket(url) ws.binaryType = "arraybuffer"; //for firefox
ws.binaryType = "arraybuffer"; //for firefox ws.onopen = () => {
ws.onopen = () => { console.log('(RTMT) ws has opened')
console.log('ws has opened') this.ws = ws
this.ws = ws onopen()
onopen() }
} ws.onclose = () => {
ws.onclose = () => { console.log('(RTMT) ws has closed')
console.log('ws has closed') //this.ws = undefined
//this.ws = undefined }
}
ws.onmessage = (event : MessageEvent)=>{ ws.onmessage = (event: MessageEvent) => {
this.onWebSocketMessage(this, event) this.onWebSocketMessage(this, event)
} }
ws.addEventListener("error", ev => { ws.addEventListener("error", ev => {
console.log({ ws_error: ev }); console.log({ ws_error: ev });
})
}) })
} }
sendMessage(channel : string, message : Int8Array) { sendMessage(channel: string, message: Bytes) {
if(this.ws === undefined){ console.log("(RTMT) Sending message to channel " + channel);
console.log('ws is undefined') if (this.ws === undefined) {
console.log('(RTMT) ws is undefined')
return return
} }
const data = encode(channel, message) const data = encode(channel, message)
console.log("(RTMT) Sending message to channel " + channel);
this.ws.send(data) this.ws.send(data)
} }
listenMessage(channel : string, callback : OnMessage) { listenMessage(channel: string, callback: OnMessage) {
this.messageChannels.set(channel, callback) this.messageChannels.set(channel, callback)
} }
onWebSocketMessage(rtmt : RTMTWS, event : MessageEvent) { onWebSocketMessage(rtmt: RTMTWS, event: MessageEvent) {
console.log(event);
const { channel, message } = decode(event.data) const { channel, message } = decode(event.data)
console.log("(RTMT) " + { channel, message });
rtmt.onMessage(channel, message) rtmt.onMessage(channel, message)
} }
onMessage(channel : string, message : Int8Array){ onMessage(channel: string, message: Bytes) {
const callback = this.messageChannels.get(channel) const callback = this.messageChannels.get(channel)
if(callback){ if (callback) {
callback(message) callback(message)
}else{ } else {
console.log("Channel callback not found!" + channel); console.log("(RTMT) Channel callback not found!" + channel);
} }
} }
} }