Created
December 1, 2016 18:18
-
-
Save andboson/6a76edf56153b7ffe25f2757c599ed0b to your computer and use it in GitHub Desktop.
min js-web-socket
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
</head> | |
<body> | |
Messages from | |
<p id="messages"></p> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> | |
<script> | |
$(document).ready(function() { | |
ws = new WebSocket("ws://localhost:8989/ws/9d971ecd-6851-403e-8ece-d81eb6b2f28a"); | |
ws.onmessage = function(event) { | |
$("#messages").append("<p>" + event.data + "</p>"); | |
}; | |
ws.onclose = function() { | |
console.log("Socket closed"); | |
}; | |
ws.onopen = function() { | |
console.log("Connected"); | |
ws.send("Hello from " + navigator.userAgent); | |
}; | |
$("#new-message").bind("submit", function(event) { | |
event.preventDefault(); | |
ws.send($("#message-text").val()); | |
$("#message-text").val(""); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment