-
Notifications
You must be signed in to change notification settings - Fork 0
/
panrpc-example-websocket-coffee-server-cli.ts
148 lines (121 loc) · 3.32 KB
/
panrpc-example-websocket-coffee-server-cli.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
136
137
138
139
140
141
142
143
144
145
146
147
148
/* eslint-disable no-console */
// eslint-disable-next-line import/no-extraneous-dependencies
import { JSONParser } from "@streamparser/json-whatwg";
// eslint-disable-next-line import/no-extraneous-dependencies
import { WebSocketServer } from "ws";
import {
ILocalContext,
IRemoteContext,
Registry,
remoteClosure,
} from "../index";
class CoffeeMachine {
constructor(private supportedVariants: string[], private waterLevel: number) {
this.BrewCoffee = this.BrewCoffee.bind(this);
}
async BrewCoffee(
ctx: ILocalContext,
variant: string,
size: number,
@remoteClosure
onProgress: (ctx: IRemoteContext, percentage: number) => Promise<void>
): Promise<number> {
if (!this.supportedVariants.includes(variant)) {
throw new Error("unsupported variant");
}
if (this.waterLevel - size < 0) {
throw new Error("not enough water");
}
console.log("Brewing coffee variant", variant, "in size", size, "ml");
await onProgress(undefined, 0);
await new Promise((r) => {
setTimeout(r, 500);
});
await onProgress(undefined, 25);
await new Promise((r) => {
setTimeout(r, 500);
});
await onProgress(undefined, 50);
await new Promise((r) => {
setTimeout(r, 500);
});
await onProgress(undefined, 75);
await new Promise((r) => {
setTimeout(r, 500);
});
await onProgress(undefined, 100);
this.waterLevel -= size;
return this.waterLevel;
}
}
class RemoteControl {
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
async SetCoffeeMachineBrewing(ctx: IRemoteContext, brewing: boolean) {}
}
let clients = 0;
const registry = new Registry(
new CoffeeMachine(["latte", "americano"], 1000),
new RemoteControl(),
{
onClientConnect: () => {
clients++;
console.log(clients, "remote controls connected");
},
onClientDisconnect: () => {
clients--;
console.log(clients, "remote controls connected");
},
}
);
const server = new WebSocketServer({
host: "127.0.0.1",
port: 1337,
});
server.on("connection", (socket) => {
socket.addEventListener("error", (e) => {
console.error("Remote control disconnected with error:", e);
});
const encoder = new WritableStream({
write(chunk) {
socket.send(JSON.stringify(chunk));
},
});
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();
});
registry.linkStream(
encoder,
decoder,
(v) => v,
(v) => v
);
});
console.log("Listening on localhost:1337");