-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
44 lines (39 loc) · 1.2 KB
/
main.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
import * as nats from "https://deno.land/x/[email protected]/src/mod.ts";
const gracefulExit = (client: nats.NatsConnection) => async () => {
await client.close();
Deno.exit();
};
if (import.meta.main) {
const client = await nats.connect({ servers: "nats://demo.nats.io:4222" });
console.log("Connected to " + client.getServer());
// const jetstream = await client.jetstreamManager();
// const stream = await jetstream.streams.add({
// name: "MUD",
// subjects: ["mud.>"],
// });
const service = await client.services.add({
name: "mud",
version: "1.0.0",
description: "Lets build a multi-user dungeon!",
});
const group = service.addGroup("mud");
group.addEndpoint("help", (err, msg) => {
if (err) {
console.error(err);
return;
}
msg.respond(
"Welcome to the MUD! Type 'nats req mud.join' to start playing.",
);
});
group.addEndpoint("join", (err, msg) => {
if (err) {
console.error(err);
return;
}
console.log("Received:", msg.headers, msg.string());
msg.respondError(420, "Not implemented");
});
Deno.addSignalListener("SIGINT", gracefulExit(client));
Deno.addSignalListener("SIGTERM", gracefulExit(client));
}