-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.js
31 lines (25 loc) · 893 Bytes
/
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
import WSClient from "../../websocket/WSClient.js";
const ws = new WSClient('ws://localhost:8887');
ws.on('close', () => console.log('Connection closed'));
await ws.connect('auth-token'); // replace with your auth token
const chatDom = document.querySelector('#chat');
const chatForm = document.querySelector('#chat-form');
const chatInput = document.querySelector('#chat-form input');
chatInput.focus();
ws.sub('chat', data => {
chatDom.insertAdjacentHTML('beforeend', `
<p>
<time>${new Date(data.time).toLocaleTimeString()}</time>
<the-user style="color: ${data.color}">
${data.user}
</the-user>
<the-msg>${data.msg}</the-msg>
</p>
`);
chatDom.scrollTop = chatDom.scrollHeight;
});
chatForm.addEventListener('submit', e => {
e.preventDefault();
ws.pub('chat', chatInput.value);
chatInput.value = '';
});