-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.html
132 lines (114 loc) · 3.78 KB
/
simple.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
crossorigin="anonymous">
</head>
<body>
<!-- a place to record messages -->
<div id="messages" class="container"></div>
<!-- load a script -->
<script type="module">
import { connect, StringCodec, JSONCodec } from './nats.js'
// add an entry to the document
function addEntry(s) {
const p = document.createElement("pre");
p.appendChild(document.createTextNode(s));
document.getElementById("messages").appendChild(p);
}
const sc = StringCodec();
const jc = JSONCodec();
const init = async function () {
// create a connection
const nc = await connect({
servers: 'ws://localhost:9222',
user: "business",
pass: "Cu62tab3ubqv1a1IEB6c8l",
})
addEntry('connected to NATS!');
window.nc = nc;
// simple publisher
nc.publish('hello', sc.encode('nats'));
addEntry('published a message to `hello`');
const scanSub0 = await nc.subscribe('localhost.*');
(async () => {
addEntry('listening for request on `localhost.*`');
for await (const m of scanSub0) {
addEntry(`Received from localhost.* ${JSON.stringify(jc.decode(m.data))}`);
}
})().then(msg => console.log('Then from localhost.*', msg)).catch(console.error);
const scanSub = await nc.subscribe('localhost.Scanner.Barcode');
(async () => {
addEntry('listening for request on `localhost.Scanner.Barcode`');
for await (const m of scanSub) {
addEntry(`Received from localhost.Scanner.Barcode ${JSON.stringify(jc.decode(m.data))}`);
}
})().then(msg => console.log('Then from localhost.Scanner.Barcode', msg)).catch(console.error);
// "recipient":
// {
// "host_name": "localhost"
// },
const scannedData = Uint8Array.from({
type: 5100,
saga:
{
id: '96d5929a-5dcb-432f-9c52-cb106af1068f',
event_id: '153affd3-3911-4193-8956-d39b6fe549ad'
},
version:
{
service: '1.0.0',
platform: '1.2.4',
format: '1.0.1'
},
info:
{
creator: 'priem_localhost',
creation_dt: '2022-02-24T18:30:47.6252176+03:00',
store_id: '140010',
host_name: 'localhost'
},
content: {
type: '2of5-int',
barcode: '10100063699368',
}
});
nc.publish('Scanner.Control', jc.encode(scannedData));
addEntry('published a message to `Scanner.Control`');
// nc.publish('localhost.Scanner.Barcode', jc.encode(scannedData));
// addEntry('published a message to `localhost.Scanner.Barcode`');
// simple subscriber, if the message has a reply subject
// send a reply
const sub = await nc.subscribe('help');
(async () => {
addEntry('listening for request on `help`');
for await (const m of sub) {
m.respond(sc.encode(`I can help ${sc.decode(m.data)}`));
}
})().then()
// const scanSub2 = await nc.subscribe('Scanner.Control');
// (async () => {
// addEntry('listening for request on `Scanner.Control`');
// for await (const m of scanSub2) {
// addEntry(`Received from Scanner.Control ${m.data}`);
// }
// })().then(console.log).catch(console.log);
// request data - requests only receive one message
// to receive multiple messages, create a subscription
addEntry('making a request to `help`');
const msg = await nc.request('help', sc.encode('nats request'))
// addEntry('making a request to `Scanner.Control`');
// const msg2 = await nc.request('Scanner.Control', jc.encode(scannedData))
addEntry(`got response '${sc.decode(msg.data)}'`);
// debugger
// addEntry(`got response '${msg2.data}'`);
// close the connection
// nc.close();
// addEntry('closed the connection');
}
init();
</script>
</body>
</html>