-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.js
84 lines (74 loc) · 2.05 KB
/
websocket.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
import { TYPING_TIMEOUT } from './settings.js';
export const inputField = document.querySelector('#chat-message-input');
export const typingIndicator = document.querySelector('#typingIndicator');
export const receiver_id = JSON.parse(document.getElementById('receiver_id').textContent);
export const sender_username = JSON.parse(document.getElementById('sender_username').textContent);
export const receiver_username = JSON.parse(document.getElementById('receiver_username').textContent);
/**
* The current typing state ('idle' or 'typing').
* @type {string}
*/
let typingState = 'idle';
/**
* Timer used to track typing activity.
* @type {number}
*/
let typingTimer;
export const messageSocket = new WebSocket(
'ws://' +
window.location.host +
'/ws/chat/' +
receiver_id +
'/'
);
export const typingSocket = new WebSocket(
'ws://' +
window.location.host +
'/ws/chat/' +
receiver_id +
'/typing/'
);
document.querySelector('#chat-form').onsubmit = (e) => {
e.preventDefault();
const message = inputField.value;
messageSocket.send(JSON.stringify({
'sender_username': sender_username,
'receiver_username': receiver_username,
'message': message,
}));
inputField.value = '';
};
/**
* Handles keydown events in the input field to track typing activity.
* @param {KeyboardEvent} e - The keyboard event.
*/
inputField.onkeydown = (e) => {
if (typingState === 'idle') {
typingSocket.send(JSON.stringify({
'sender_username': sender_username,
'typing': true,
}));
typingState = 'typing';
}
};
/**
* Stops tracking typing activity when the user stops typing.
*/
const stopTyping = () => {
if (typingState === 'typing') {
typingSocket.send(JSON.stringify({
'sender_username': sender_username,
'typing': false,
}));
typingState = 'idle';
}
};
/**
* Handles input events in the input field to stop tracking typing activity after a timeout.
*/
inputField.oninput = () => {
clearTimeout(typingTimer);
typingTimer = setTimeout(() => {
stopTyping();
}, TYPING_TIMEOUT);
};