Skip to content

Instantly share code, notes, and snippets.

@dmateos
Created September 2, 2011 00:14
Show Gist options
  • Save dmateos/1187645 to your computer and use it in GitHub Desktop.
Save dmateos/1187645 to your computer and use it in GitHub Desktop.
<html>
<script type="text/javascript"
src="http://code.jquery.com/jquery-latest.pack.js">
</script>
<script>
$(document).ready(function() {
function debug(str) {
$("#debug").append(str + "<br/>");
};
if ("WebSocket" in window) {
var server = "ws://localhost:8080";
debug("connecting to " + server);
var socket = new WebSocket(server);
sendfrominputbox = function() {
var message = $("#input").val();
socket.send(message);
$("#input").val("");
};
socket.onopen = function() {
debug("connected");
};
socket.onmessage = function(message) {
debug(message.data);
};
socket.onclose = function() {
debug("connection died");
};
socket.onerror = function() {
debug("error");
};
}
else {
debug("browser doesnt support websockets");
}
});
</script>
<head>
<title></title>
</head>
<body>
<div id="debug">
</div>
<input id="input" onchange="sendfrominputbox()" size="42"></input>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment