Skip to content

Instantly share code, notes, and snippets.

@sowich
Created January 21, 2019 05:57
Show Gist options
  • Save sowich/9a2d864684deda417bfde9be9a9e1a3d to your computer and use it in GitHub Desktop.
Save sowich/9a2d864684deda417bfde9be9a9e1a3d to your computer and use it in GitHub Desktop.
javascript
"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