Created
September 26, 2016 11:46
-
-
Save MagnusThor/31510bfbf612c2c6e408fdc8a4673cf6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class BinaryMessage { | |
Buffer: ArrayBuffer; | |
private header: Uint8Array | |
GetMessage(buffer: ArrayBuffer): any { | |
let headerLen = 8; | |
let header = new Uint8Array(buffer, 0, headerLen); | |
let payloadLength = ThorIO.Client.Utils.arrayToLong(header); | |
let message = new Uint8Array(buffer, headerLen, payloadLength); | |
let blobOffset = headerLen + payloadLength; | |
let messageBuffer = new Uint8Array(buffer, blobOffset, buffer.byteLength - blobOffset); | |
return String.fromCharCode.apply(null, new Uint16Array(message)); | |
} | |
constructor(message: string, public arrayBuffer: ArrayBuffer) { | |
this.header = new Uint8Array(ThorIO.Client.Utils.longToArray(message.length)); | |
this.Buffer = this.joinBuffers(this.joinBuffers( | |
this.header, | |
ThorIO.Client.Utils.stingToBuffer(message)), | |
arrayBuffer); | |
} | |
private joinBuffers(a: ArrayBuffer, b: ArrayBuffer): ArrayBuffer { | |
let newBuffer = new Uint8Array(a.byteLength + b.byteLength); | |
newBuffer.set(new Uint8Array(a), 0); | |
newBuffer.set(new Uint8Array(b), a.byteLength); | |
return newBuffer.buffer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment