Reset retry delay to 1 second when a connection has succeeded

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2024-04-28 15:09:52 +02:00
parent 675f19492c
commit c6b9a84def
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 42 additions and 1 deletions

View File

@ -150,6 +150,22 @@ function receiveFetchedMessages(data) {
redrawMessages() redrawMessages()
} }
function startPrivateChat(data) {
let channel = data['channel']
if (!channel) {
console.error('Private chat not found:', data)
return
}
if (!channels[channel['id']]) {
channels[channel['id']] = channel
messages[channel['id']] = new Map()
setChannels(Object.values(channels))
}
selectChannel(channel['id'])
}
function redrawMessages() { function redrawMessages() {
let messageList = document.getElementById('message-list') let messageList = document.getElementById('message-list')
messageList.innerHTML = '' messageList.innerHTML = ''
@ -181,6 +197,26 @@ function redrawMessages() {
authorSpan.innerText = message['author'] authorSpan.innerText = message['author']
authorDiv.appendChild(authorSpan) authorDiv.appendChild(authorSpan)
authorSpan.addEventListener('contextmenu', (menu_event) => {
menu_event.preventDefault()
const popover = bootstrap.Popover.getOrCreateInstance(authorSpan, {
'title': message['author'],
'content': `<a id="send-private-message-link-${message['id']}" class="nav-link" href="#" tabindex="0">Envoyer un message privé</a>`,
'html': true,
'placement': "bottom",
})
popover.show()
document.getElementById('send-private-message-link-' + message['id']).addEventListener('click', event => {
event.preventDefault()
popover.hide()
socket.send(JSON.stringify({
'type': 'start_private_chat',
'user_id': message['author_id'],
}))
})
})
let dateSpan = document.createElement('span') let dateSpan = document.createElement('span')
dateSpan.classList.add('text-muted', 'float-end') dateSpan.classList.add('text-muted', 'float-end')
dateSpan.innerText = new Date(message['timestamp']).toLocaleString() dateSpan.innerText = new Date(message['timestamp']).toLocaleString()
@ -234,6 +270,9 @@ document.addEventListener('DOMContentLoaded', () => {
case 'fetch_messages': case 'fetch_messages':
receiveFetchedMessages(data) receiveFetchedMessages(data)
break break
case 'start_private_chat':
startPrivateChat(data)
break
default: default:
console.log(data) console.log(data)
console.error('Unknown message type:', data['type']) console.error('Unknown message type:', data['type'])
@ -246,6 +285,7 @@ document.addEventListener('DOMContentLoaded', () => {
socket = new WebSocket( socket = new WebSocket(
(document.location.protocol === 'https:' ? 'wss' : 'ws') + '://' + window.location.host + '/ws/chat/' (document.location.protocol === 'https:' ? 'wss' : 'ws') + '://' + window.location.host + '/ws/chat/'
) )
let socketOpen = false
// Listen on websockets and process messages from the server // Listen on websockets and process messages from the server
socket.addEventListener('message', e => { socket.addEventListener('message', e => {
@ -258,10 +298,11 @@ document.addEventListener('DOMContentLoaded', () => {
// Manage errors // Manage errors
socket.addEventListener('close', e => { socket.addEventListener('close', e => {
console.error('Chat socket closed unexpectedly, restarting…') console.error('Chat socket closed unexpectedly, restarting…')
setTimeout(() => setupSocket(2 * nextDelay), nextDelay) setTimeout(() => setupSocket(socketOpen ? 1000 : 2 * nextDelay), nextDelay)
}) })
socket.addEventListener('open', e => { socket.addEventListener('open', e => {
socketOpen = true
socket.send(JSON.stringify({ socket.send(JSON.stringify({
'type': 'fetch_channels', 'type': 'fetch_channels',
})) }))