Created
January 21, 2019 05:57
-
-
Save sowich/9a2d864684deda417bfde9be9a9e1a3d to your computer and use it in GitHub Desktop.
javascript
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
"use strict"; | |
fetch("...", {method: "POST"}) | |
.then(result => result.json()) | |
.then(result => { | |
let ws = new WebSocket("..." + result.connectionId); | |
ws.onopen = () => { | |
ws.send(JSON.stringify({...})); | |
ws.send(JSON.stringify({...})); | |
}; | |
ws.onmessage = (event) => { | |
let result = JSON.parse(event.data); | |
if (result.type === 3) { | |
let event = new CustomEvent('customevent', {'detail': result}); | |
document.dispatchEvent(event); | |
} | |
}; | |
ws.onerror = (error) => console.log(error); | |
ws.onclose = (event) => event.wasClean ? console.log('Connection is closed!') : console.log('Reset connection!'); | |
}) | |
.catch(error => console.error(error)); | |
document.addEventListener('customevent', (result) => console.log(result)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment