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 interface RTMT{

View File

@ -1,5 +1,5 @@
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";
@ -12,18 +12,17 @@ export class RTMTWS implements RTMT{
this.messageChannels = new Map<String, OnMessage>()
}
initWebSocket(onopen : ()=>any) {
context.userKeyStore.getUserKey((userKey : string)=>{
initWebSocket(userKey: string, onopen: () => any) {
const url = wsServerAdress + '?userKey=' + userKey
const ws = new WebSocket(url)
ws.binaryType = "arraybuffer"; //for firefox
ws.onopen = () => {
console.log('ws has opened')
console.log('(RTMT) ws has opened')
this.ws = ws
onopen()
}
ws.onclose = () => {
console.log('ws has closed')
console.log('(RTMT) ws has closed')
//this.ws = undefined
}
@ -34,16 +33,15 @@ export class RTMTWS implements RTMT{
ws.addEventListener("error", ev => {
console.log({ ws_error: ev });
})
})
}
sendMessage(channel : string, message : Int8Array) {
sendMessage(channel: string, message: Bytes) {
console.log("(RTMT) Sending message to channel " + channel);
if (this.ws === undefined) {
console.log('ws is 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)
}
@ -52,19 +50,18 @@ export class RTMTWS implements RTMT{
}
onWebSocketMessage(rtmt: RTMTWS, event: MessageEvent) {
console.log(event);
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) {
callback(message)
} else {
console.log("Channel callback not found!" + channel);
console.log("(RTMT) Channel callback not found!" + channel);
}
}
}