This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathRawWebsocketMessage.ts
56 lines (43 loc) · 1.56 KB
/
RawWebsocketMessage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { MessageType } from "./ConnectionMessage";
import { ArgumentNullError, InvalidOperationError } from "./Error";
import { CreateNoDashGuid } from "./Guid";
export class RawWebsocketMessage {
private messageType: MessageType;
private payload: any = null;
private id: string;
public constructor(messageType: MessageType, payload: any, id?: string) {
if (!payload) {
throw new ArgumentNullError("payload");
}
if (messageType === MessageType.Binary && !(payload instanceof ArrayBuffer)) {
throw new InvalidOperationError("Payload must be ArrayBuffer");
}
if (messageType === MessageType.Text && !(typeof (payload) === "string")) {
throw new InvalidOperationError("Payload must be a string");
}
this.messageType = messageType;
this.payload = payload;
this.id = id ? id : CreateNoDashGuid();
}
public get MessageType(): MessageType {
return this.messageType;
}
public get Payload(): any {
return this.payload;
}
public get TextContent(): string {
if (this.messageType === MessageType.Binary) {
throw new InvalidOperationError("Not supported for binary message");
}
return this.payload as string;
}
public get BinaryContent(): ArrayBuffer {
if (this.messageType === MessageType.Text) {
throw new InvalidOperationError("Not supported for text message");
}
return this.payload;
}
public get Id(): string {
return this.id;
}
}