-
Notifications
You must be signed in to change notification settings - Fork 29
/
chat.js
122 lines (108 loc) · 3.28 KB
/
chat.js
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
/*
* Copyright 2020 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { connect, JSONCodec } from "../esm/nats.js";
const me = Date.now();
window.chat = {
send: send,
exiting: exiting,
};
// create a decoder, the client is sending JSON
const jc = JSONCodec();
// create a connection, and register listeners
const init = async function () {
// if the connection doesn't resolve, an exception is thrown
// a real app would allow configuring the hostport and whether
// to use WSS or not.
const conn = await connect(
{ servers: "ws://localhost:9222" },
);
// handle connection to the server is closed - should disable the ui
conn.closed().then((err) => {
let m = "NATS connection closed";
addEntry(`${m} ${err ? err.message : ""}`);
});
(async () => {
for await (const s of conn.status()) {
addEntry(`Received status update: ${s.type}`);
}
})().then();
// the chat application listens for messages sent under the subject 'chat'
(async () => {
const chat = conn.subscribe("chat");
for await (const m of chat) {
const jm = jc.decode(m.data);
addEntry(
jm.id === me ? `(me): ${jm.m}` : `(${jm.id}): ${jm.m}`,
);
}
})().then();
// when a new browser joins, the joining browser publishes an 'enter' message
(async () => {
const enter = conn.subscribe("enter");
for await (const m of enter) {
const jm = jc.decode(m.data);
addEntry(`${jm.id} entered.`);
}
})().then();
(async () => {
const exit = conn.subscribe("exit");
for await (const m of exit) {
const jm = jc.decode(m.data);
if (jm.id !== me) {
addEntry(`${jm.id} exited.`);
}
}
})().then();
// we connected, and we publish our enter message
conn.publish("enter", jc.encode({ id: me }));
return conn;
};
init().then((conn) => {
window.nc = conn;
}).catch((ex) => {
addEntry(`Error connecting to NATS: ${ex}`);
});
// this is the input field
let input = document.getElementById("data");
// add a listener to detect edits. If they hit Enter, we publish it
input.addEventListener("keyup", (e) => {
if (e.key === "Enter") {
document.getElementById("send").click();
} else {
e.preventDefault();
}
});
// send a message if user typed one
function send() {
input = document.getElementById("data");
const m = input.value;
if (m !== "" && window.nc) {
window.nc.publish("chat", jc.encode({ id: me, m: m }));
input.value = "";
}
return false;
}
// send the exit message
function exiting() {
if (window.nc) {
window.nc.publish("exit", jc.encode({ id: me }));
}
}
// add an entry to the document
function addEntry(s) {
const p = document.createElement("pre");
p.appendChild(document.createTextNode(s));
document.getElementById("chats").appendChild(p);
}