Commit 43459568 authored by Aral Balkan's avatar Aral Balkan

Add client-side connection sanity check before firing a send message

parent d5dab7d4
......@@ -3,8 +3,6 @@ window.addEventListener('load', _ => {
$ = document.querySelector.bind(document)
const Modal = Swal
const socket = new WebSocket(`wss://${window.location.hostname}/patron`)
// Handle connectivity.
......@@ -120,6 +118,13 @@ window.addEventListener('load', _ => {
itemId
}
// Sanity check: socket must be open.
// (Note: our validation should have caught this by this stage and it’s a bug if it hasn’t.)
if (socket.readyState !== WebSocket.OPEN) {
Modal.fire('Not connected', `Sorry, we cannot ${command.action} your patronage right now as we cannot connect to the server.`, 'error')
return
}
if (commandName === 'update') {
command.amount = $('#patronageAmount').value
$('#update-button').value = 'Updating…'
......@@ -145,7 +150,12 @@ window.addEventListener('load', _ => {
$('#cancel-button').value = 'Cancelling…'
}
socket.send(JSON.stringify(command))
try {
socket.send(JSON.stringify(command))
} catch (error) {
console.log('Cannot send message', error)
Modal.fire('Cannot send message', error, 'error')
}
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment