Skip to content

Instantly share code, notes, and snippets.

@pk11
Created August 7, 2013 22:14
Show Gist options
  • Save pk11/6179306 to your computer and use it in GitHub Desktop.
Save pk11/6179306 to your computer and use it in GitHub Desktop.
<html>
<body>
<!-- Send text to websocket -->
<input id="userInput" type="text">
<button onclick="ws.send(document.getElementById('userInput').value)">Send</button>
<h1>Websockets demo</h1>
<!-- Results -->
<div id="message"></div>
<script>
function showMessage(text) {
document.getElementById('message').innerHTML = text;
}
var ws = new WebSocket('ws://' + document.location.host + '/websockets');
showMessage('Connecting...');
ws.onopen = function() { showMessage('Connected!'); };
ws.onclose = function() { showMessage('Lost connection'); };
ws.onmessage = function(msg) { showMessage(msg.data); };
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment