added rtmt

This commit is contained in:
jhalitaksoy 2021-06-29 03:25:42 +03:00
parent e0fd248466
commit c0c1238b25
3 changed files with 19 additions and 21 deletions

View File

@ -4,14 +4,15 @@ import { Bytes } from "./rtmt";
const headerLenght = 4 const headerLenght = 4
export type Message = { export type Message = {
channel : string, channel: string,
data : Bytes, data: Bytes,
} }
// //
// channel is string, message is byte array // channel is string, message is byte array
// //
export function encode(message : Message) { export function encode(message: Message) {
const {channel, data} = message const { channel, data } = message
const channelLenght = channel.length const channelLenght = channel.length
const messageLenght = data.length const messageLenght = data.length
const totalLenght = headerLenght + channelLenght + messageLenght const totalLenght = headerLenght + channelLenght + messageLenght
@ -24,15 +25,15 @@ export function encode(message : Message) {
const channelBytes = encodeText(channel) const channelBytes = encodeText(channel)
let count = headerLenght let count = headerLenght
channelBytes.forEach((byte : any)=>{ channelBytes.forEach((byte: any) => {
view.setUint8(count, byte) view.setUint8(count, byte)
count++ count++
}) })
for (const byte of data) { data.forEach((byte: any) => {
view.setUint8(count, byte) view.setUint8(count, byte)
count++ count++
} })
return buffer return buffer
} }
@ -40,16 +41,12 @@ export function encode(message : Message) {
// //
// return { channel : string, message : byte array} // return { channel : string, message : byte array}
// //
export function decode(bytes : Bytes) : Message { export function decode(bytes: Bytes): Message {
const view = new DataView(bytes.buffer); const channelLenght = bytes.readInt32BE(0)
const channelLenght = view.getUint32(0)
const channel = decodeText( const channel = decodeText(
bytes.slice(headerLenght, headerLenght + channelLenght)) bytes.slice(headerLenght, headerLenght + channelLenght))
const message = bytes.slice(headerLenght + channelLenght) const message = bytes.slice(headerLenght + channelLenght)
return { return {
"channel": channel, "channel": channel,

View File

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

View File

@ -17,19 +17,18 @@ export class RTMTWS implements RTMT {
this.wsServer = null this.wsServer = null
} }
initWebSocket(server: http.Server, onopen: () => any) { initWebSocket(server: http.Server, onopen: (userKey : string) => any) {
const wsServer = new WebSocket.Server({ server }) const wsServer = new WebSocket.Server({ server })
this.wsServer = wsServer
this.clients = new Map<string, WebSocket>() this.clients = new Map<string, WebSocket>()
wsServer.on("connection", (ws: WebSocket, req: Request) => { wsServer.on("connection", (ws: WebSocket, req: Request) => {
console.log(req.url);
const regexResult = req.url.split(RegExp("\/\?userKey=")); const regexResult = req.url.split(RegExp("\/\?userKey="));
const clientID = regexResult[1] const clientID = regexResult[1]
console.log(regexResult);
this.clients.set(clientID, ws) this.clients.set(clientID, ws)
ws.on("message", (messageBytes: Bytes) => { ws.on("message", (messageBytes: Bytes) => {
console.log('received: %s', messageBytes); console.log('received: %s', messageBytes);
this.onWebSocketMessage("0", messageBytes) this.onWebSocketMessage(clientID, messageBytes)
}) })
ws.on("close", (code: number, reason: string) => { ws.on("close", (code: number, reason: string) => {
@ -41,6 +40,8 @@ export class RTMTWS implements RTMT {
console.log("WS Closed with error! error : " + err.message); console.log("WS Closed with error! error : " + err.message);
this.clients.delete(clientID) this.clients.delete(clientID)
}) })
onopen(clientID)
}) })
} }
@ -70,7 +71,7 @@ export class RTMTWS implements RTMT {
} }
onMessage(clientID: string, channel: string, message: Int8Array) { onMessage(clientID: string, channel: string, message: Bytes) {
const callback = this.messageChannels.get(channel) const callback = this.messageChannels.get(channel)
if (callback) { if (callback) {
callback(clientID, message) callback(clientID, message)