Created
May 10, 2017 02:46
-
-
Save jean-lourenco/724ecb202acc661b88b4eda0e49f9ca9 to your computer and use it in GitHub Desktop.
Test any websocket endpoint
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
<html> | |
<head> | |
<title>r/place clone</title> | |
</head> | |
<body> | |
<input type="text" value="" id="message" /> | |
<input type="button" value="Send" onclick="send()" /> | |
<div id="container"></div> | |
<script> | |
var ws = new WebSocket('ws://localhost:5001/ws'); | |
var messageInput = document.getElementById('message'); | |
var container = document.getElementById('container'); | |
var wsOpen = false; | |
ws.onopen = (event) => { | |
wsOpen = true; | |
console.log('connection opened'); | |
} | |
ws.onclose = (event) => { | |
wsOpen = false; | |
console.log('connection closed!'); | |
} | |
ws.onmessage = (event) => { | |
container.innerHTML += '<div>' + event.data + '</div>'; | |
} | |
function send() { | |
if (wsOpen) { | |
ws.send(messageInput.value); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment