Skip to content

Instantly share code, notes, and snippets.

@andboson
Created December 1, 2016 18:18
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 andboson/6a76edf56153b7ffe25f2757c599ed0b to your computer and use it in GitHub Desktop.
Save andboson/6a76edf56153b7ffe25f2757c599ed0b to your computer and use it in GitHub Desktop.
min js-web-socket
<!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