forked from jdiamond/MQTT.ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwt.ts
executable file
·38 lines (28 loc) · 827 Bytes
/
lwt.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
#!/usr/bin/env -S deno run --allow-net
import { Client } from "../mod.ts";
async function main() {
const client1 = new Client({
url: "mqtt://localhost:1883",
});
await client1.connect();
console.log("Client 1 Connected");
await client1.subscribe("lwt");
client1.on("message", (topic: string, payload: Uint8Array) => {
console.log("Recieved:", topic, new TextDecoder().decode(payload));
client1.disconnect().then(() => void Deno.exit(0));
});
const client2 = new Client({
url: "mqtt://localhost:1883",
will: {
qos: 1,
retain: true,
topic: "lwt",
payload: "Client2 has crashed",
},
});
await client2.connect();
console.log("Client 2 Connected");
//@ts-expect-error Accessing Private variable to 'crash' client 2
client2.conn?.close();
}
main();