-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanrpc-example-websocket-coffee-client-cli.ts
More file actions
214 lines (175 loc) · 5.42 KB
/
panrpc-example-websocket-coffee-client-cli.ts
File metadata and controls
214 lines (175 loc) · 5.42 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* eslint-disable no-console */
import { exit, stdin, stdout } from "process";
import { createInterface } from "readline/promises";
// eslint-disable-next-line import/no-extraneous-dependencies
import { JSONParser } from "@streamparser/json-whatwg";
// eslint-disable-next-line import/no-extraneous-dependencies
import { WebSocket } from "ws";
import { ILocalContext, IRemoteContext, Registry } from "../index";
class RemoteControl {
// eslint-disable-next-line class-methods-use-this
async SetCoffeeMachineBrewing(ctx: ILocalContext, brewing: boolean) {
if (brewing) {
console.log("Coffee machine is now brewing");
} else {
console.log("Coffee machine has stopped brewing");
}
}
}
class TeaBrewer {
// eslint-disable-next-line @typescript-eslint/no-unused-vars, class-methods-use-this
async GetVariants(ctx: IRemoteContext): Promise<string[]> {
return [];
}
}
class CoffeeMachine<E> {
constructor(public Extension: E) {}
// eslint-disable-next-line class-methods-use-this
async BrewCoffee(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ctx: IRemoteContext,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
variant: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
size: number,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onProgress: (ctx: ILocalContext, percentage: number) => Promise<void>
): Promise<number> {
return 0;
}
}
let clients = 0;
const registry = new Registry(
new RemoteControl(),
new CoffeeMachine(new TeaBrewer()),
{
onClientConnect: () => {
clients++;
console.log(clients, "coffee machines connected");
},
onClientDisconnect: () => {
clients--;
console.log(clients, "coffee machines connected");
},
}
);
(async () => {
console.log(`Enter one of the following numbers followed by <ENTER> to brew a coffee:
- 1: Brew small Cafè Latte
- 2: Brew large Cafè Latte
- 3: Brew small Americano
- 4: Brew large Americano
Or enter 5 to list available tea variants.`);
const rl = createInterface({ input: stdin, output: stdout });
// eslint-disable-next-line no-constant-condition
while (true) {
const line =
// eslint-disable-next-line no-await-in-loop
await rl.question("");
// eslint-disable-next-line no-await-in-loop
await registry.forRemotes(async (remoteID, remote) => {
switch (line) {
case "1":
case "2":
try {
// eslint-disable-next-line no-await-in-loop
const res = await remote.BrewCoffee(
undefined,
"latte",
line === "1" ? 100 : 200,
async (ctx, percentage) =>
console.log(`Brewing Cafè Latte ... ${percentage}% done`)
);
console.log("Remaining water:", res, "ml");
} catch (e) {
console.error(`Couldn't brew Cafè Latte: ${e}`);
}
break;
case "3":
case "4":
try {
// eslint-disable-next-line no-await-in-loop
const res = await remote.BrewCoffee(
undefined,
"americano",
line === "3" ? 100 : 200,
async (ctx, percentage) =>
console.log(`Brewing Americano ... ${percentage}% done`)
);
console.log("Remaining water:", res, "ml");
} catch (e) {
console.error(`Couldn't brew Americano: ${e}`);
}
break;
case "5":
try {
// eslint-disable-next-line no-await-in-loop
const res = await remote.Extension.GetVariants(undefined);
console.log("Available tea variants:", res);
} catch (e) {
console.error(`Couldn't list available tea variants: ${e}`);
}
break;
default:
console.log(`Unknown letter ${line}, ignoring input`);
}
});
}
})();
// Connect to WebSocket server
const socket = new WebSocket("ws://127.0.0.1:1337");
socket.addEventListener("error", (e) => {
console.error("Disconnected with error:", e);
exit(1);
});
socket.addEventListener("close", () => exit(0));
await new Promise<void>((res, rej) => {
socket.addEventListener("open", () => res());
socket.addEventListener("error", rej);
});
const linkSignal = new AbortController();
// Set up streaming JSON encoder
const encoder = new WritableStream({
write(chunk) {
socket.send(JSON.stringify(chunk));
},
});
// Set up streaming JSON decoder
const parser = new JSONParser({
paths: ["$"],
separator: "",
});
const parserWriter = parser.writable.getWriter();
const parserReader = parser.readable.getReader();
const decoder = new ReadableStream({
start(controller) {
parserReader
.read()
.then(async function process({ done, value }) {
if (done) {
controller.close();
return;
}
controller.enqueue(value?.value);
parserReader
.read()
.then(process)
.catch((e) => controller.error(e));
})
.catch((e) => controller.error(e));
},
});
socket.addEventListener("message", (m) => parserWriter.write(m.data as string));
socket.addEventListener("close", () => {
parserReader.cancel();
parserWriter.abort();
linkSignal.abort();
});
registry.linkStream(
linkSignal.signal,
encoder,
decoder,
(v) => v,
(v) => v
);
console.log("Connected to localhost:1337");