added rtmt
This commit is contained in:
parent
e0fd248466
commit
c0c1238b25
@ -7,6 +7,7 @@ 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
|
||||||
//
|
//
|
||||||
@ -29,10 +30,10 @@ export function encode(message : Message) {
|
|||||||
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
|
||||||
}
|
}
|
||||||
@ -41,11 +42,7 @@ 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))
|
||||||
|
|
||||||
|
|||||||
@ -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{
|
||||||
|
|||||||
@ -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)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user