-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
websocket: add notifications and reconnect on error
- Loading branch information
1 parent
7050088
commit fdb19e5
Showing
5 changed files
with
72 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
writable | ||
} from 'svelte/store'; | ||
|
||
const status = writable(''); | ||
const message = writable(''); | ||
|
||
function initSocket() { | ||
const socket = new WebSocket(`ws://${location.hostname}:${BACKEND_PORT}/wol/`); | ||
|
||
// Connection opened | ||
socket.addEventListener('open', function () { | ||
status.set("open"); | ||
}); | ||
|
||
// Connection closed | ||
socket.addEventListener('close', function () { | ||
status.set("close"); | ||
setTimeout(function () { | ||
initSocket(); | ||
}, 3000); | ||
}); | ||
|
||
// Connection error | ||
socket.addEventListener('error', function () { | ||
status.set("error"); | ||
socket.close() | ||
}); | ||
|
||
// Listen for messages | ||
socket.addEventListener('message', function (event) { | ||
message.set(JSON.parse(event.data)); | ||
}); | ||
} | ||
|
||
initSocket() | ||
|
||
const sendMessage = (message) => { | ||
if (socket.readyState <= 1) { | ||
socket.send(JSON.stringify(message)); | ||
} | ||
} | ||
|
||
export default { | ||
subscribeMsg: message.subscribe, | ||
subscribeStatus: status.subscribe, | ||
sendMessage | ||
} |
This file was deleted.
Oops, something went wrong.