Created
March 25, 2019 13:27
-
-
Save yottta/0ebe18cb3b49079c6c3c7c3eb70db264 to your computer and use it in GitHub Desktop.
Medium article about WebSockets throughout Varnish - initial web client
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> | |
<head> | |
<title>Products</title> | |
</head> | |
<body> | |
<p>Product List:</p> | |
<ul id="products"> | |
</ul> | |
<script> | |
const httpGET = new XMLHttpRequest(); | |
httpGET.open("GET", "http://127.0.0.1:8080/product"); | |
httpGET.send(); | |
httpGET.onloadend = (e) => { | |
var products = JSON.parse(httpGET.responseText); | |
products['products'].forEach(function(el) { | |
existingContent = document.getElementById("products").innerHTML | |
document.getElementById("products").innerHTML = existingContent + ("<li>" + el + "</li>"); | |
}); | |
} | |
let socket = new WebSocket("ws://127.0.0.1:8080/ws") | |
socket.onopen = () => { | |
console.log("Successfully websocket connected") | |
} | |
socket.onclose = (event) => { | |
console.log("Socket closed connection", event) | |
} | |
socket.onerror = (error) => { | |
console.log("Socket error: ", error) | |
} | |
socket.onmessage = (msgEvent) => { | |
console.log("Message received", msgEvent.data) | |
existingContent = document.getElementById("products").innerHTML | |
document.getElementById("products").innerHTML = existingContent + ("<li>" + msgEvent.data + "</li>"); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment