-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobsWs.ts
135 lines (130 loc) · 4.47 KB
/
obsWs.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import ws, { WebSocket } from "ws";
export class OBSws {
address: string;
logging: boolean;
callbacks: any;
sends: any;
currentScene: string;
sceneList: never[];
ws: import("ws");
resolve: any;
constructor(address: string, logging = false) {
this.address = address;
this.logging = logging;
this.callbacks = {};
this.sends = {};
this.currentScene = "";
this.sceneList = [];
this.ws = new WebSocket(this.address);
this.connect();
}
connect() {
this.ws = new WebSocket(this.address);
this.ws.onopen = () => {
if (this.resolve) this.resolve();
this.send("GetAuthRequired").then((msg) => {
if (msg.authRequired) {
throw new Error("Not implemented");
} else if (this.callbacks["ConnectionOpened"])
for (const callback of this.callbacks["ConnectionOpened"]) {
callback();
}
});
};
this.ws.onclose = (e: { reason: any }) => {
if (!e.reason) {
console.log(
"Socket is closed. Reconnect will be attempted in 5 seconds.",
e.reason,
);
setTimeout(() => {
this.connect();
}, 5000);
}
};
this.ws.onerror = (err: { message: any }) => {
console.error(
"Socket encountered error: ",
err.message,
"Closing socket",
);
this.ws.close();
};
this.ws.onmessage = (message: ws.MessageEvent) => {
const msg = JSON.parse(message.data.toString());
// console.log(msg)
if (this.callbacks[msg["update-type"]])
for (const callback of this.callbacks[msg["update-type"]]) {
callback(msg);
}
if (this.sends[msg["message-id"]]) {
this.sends[msg["message-id"]](msg);
delete this.sends[msg["message-id"]];
}
};
return new Promise((resolve, reject) => {
try {
this.resolve = resolve;
} catch (e) {
reject(e);
}
});
}
clearCallbacks() {
this.callbacks = {};
}
on(type: string | number, callback: any) {
if (this.callbacks[type] == null) this.callbacks[type] = [];
this.callbacks[type].push(callback);
}
send(type: string, options: any = {}): Promise<any> {
if (this.ws.readyState === WebSocket.OPEN)
return new Promise((resolve, reject) => {
try {
const mid = Math.random().toString(36).substring(7);
if (this.logging)
console.log(
"sending",
Object.assign(
{ "request-type": type, "message-id": mid },
options,
),
);
this.ws.send(
JSON.stringify(
Object.assign(
{ "request-type": type, "message-id": mid },
options,
),
),
);
this.sends[mid] = resolve;
} catch (e) {
reject(e);
}
});
else
return new Promise((resolve, _reject) => {
this.connect().then(() => {
const mid = Math.random().toString(36).substring(7);
if (this.logging)
console.log(
"sending",
Object.assign(
{ "request-type": type, "message-id": mid },
options,
),
);
this.ws.send(
JSON.stringify(
Object.assign(
{ "request-type": type, "message-id": mid },
options,
),
),
);
this.sends[mid] = resolve;
});
});
}
}