added rtmt
This commit is contained in:
parent
e0fd248466
commit
c0c1238b25
@ -4,14 +4,15 @@ import { Bytes } from "./rtmt";
|
||||
const headerLenght = 4
|
||||
|
||||
export type Message = {
|
||||
channel : string,
|
||||
data : Bytes,
|
||||
channel: string,
|
||||
data: Bytes,
|
||||
}
|
||||
|
||||
//
|
||||
// channel is string, message is byte array
|
||||
//
|
||||
export function encode(message : Message) {
|
||||
const {channel, data} = message
|
||||
export function encode(message: Message) {
|
||||
const { channel, data } = message
|
||||
const channelLenght = channel.length
|
||||
const messageLenght = data.length
|
||||
const totalLenght = headerLenght + channelLenght + messageLenght
|
||||
@ -24,15 +25,15 @@ export function encode(message : Message) {
|
||||
const channelBytes = encodeText(channel)
|
||||
|
||||
let count = headerLenght
|
||||
channelBytes.forEach((byte : any)=>{
|
||||
view.setUint8(count, byte)
|
||||
channelBytes.forEach((byte: any) => {
|
||||
view.setUint8(count, byte)
|
||||
count++
|
||||
})
|
||||
|
||||
for (const byte of data) {
|
||||
data.forEach((byte: any) => {
|
||||
view.setUint8(count, byte)
|
||||
count++
|
||||
}
|
||||
})
|
||||
|
||||
return buffer
|
||||
}
|
||||
@ -40,16 +41,12 @@ export function encode(message : Message) {
|
||||
//
|
||||
// return { channel : string, message : byte array}
|
||||
//
|
||||
export function decode(bytes : Bytes) : Message {
|
||||
const view = new DataView(bytes.buffer);
|
||||
|
||||
const channelLenght = view.getUint32(0)
|
||||
|
||||
|
||||
export function decode(bytes: Bytes): Message {
|
||||
const channelLenght = bytes.readInt32BE(0)
|
||||
const channel = decodeText(
|
||||
bytes.slice(headerLenght, headerLenght + channelLenght))
|
||||
|
||||
const message = bytes.slice(headerLenght + channelLenght)
|
||||
const message = bytes.slice(headerLenght + channelLenght)
|
||||
|
||||
return {
|
||||
"channel": channel,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export type Bytes = UInt8Array
|
||||
export type Bytes = Buffer
|
||||
export type OnMessage = (clientID : string, message : Bytes) => any
|
||||
|
||||
export interface RTMT{
|
||||
|
||||
@ -17,19 +17,18 @@ export class RTMTWS implements RTMT {
|
||||
this.wsServer = null
|
||||
}
|
||||
|
||||
initWebSocket(server: http.Server, onopen: () => any) {
|
||||
initWebSocket(server: http.Server, onopen: (userKey : string) => any) {
|
||||
const wsServer = new WebSocket.Server({ server })
|
||||
this.wsServer = wsServer
|
||||
this.clients = new Map<string, WebSocket>()
|
||||
|
||||
wsServer.on("connection", (ws: WebSocket, req: Request) => {
|
||||
console.log(req.url);
|
||||
const regexResult = req.url.split(RegExp("\/\?userKey="));
|
||||
const clientID = regexResult[1]
|
||||
console.log(regexResult);
|
||||
this.clients.set(clientID, ws)
|
||||
ws.on("message", (messageBytes: Bytes) => {
|
||||
console.log('received: %s', messageBytes);
|
||||
this.onWebSocketMessage("0", messageBytes)
|
||||
this.onWebSocketMessage(clientID, messageBytes)
|
||||
})
|
||||
|
||||
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);
|
||||
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)
|
||||
if (callback) {
|
||||
callback(clientID, message)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user