Skip to content

Instantly share code, notes, and snippets.

@karenpeng
Last active March 19, 2019 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karenpeng/3bb187bc734f12ac828cca06a2825c0e to your computer and use it in GitHub Desktop.
Save karenpeng/3bb187bc734f12ac828cca06a2825c0e to your computer and use it in GitHub Desktop.
const wsConnection = new WebSocket('ws://localhost:3000/websocket');
wsConnection.onopen = () => {
console.log('websocket connection is opened')
wsConnection.send('A');
};
wsConnection.onmessage = event => {
if (event.data === '9') {
setTimeout(() => {
wsConnection.send('A');
}, 3000);
}
}
wsConnection.onerror = event => {
if (event.readyState === WebSocket.CLOSED) {
console.log('websocket connection is closed')
}
}
setTimeout(() => {
console.log('client closes the websocket connection');
wsConnection.close();
}, 30000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment