Created
December 4, 2017 17:59
-
-
Save AndyNovo/ae62899433e30d17ae731d2d575d334c to your computer and use it in GitHub Desktop.
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> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>WebSocket Test</title> | |
</head> | |
<body> | |
<h1>Hi there</h1> | |
<input type="text" id="msg"/> | |
<button type="button" id="send">Click to Send</button> | |
<ul id="msgs"> | |
</ul> | |
<script> | |
var ws; | |
window.onload=function(){ | |
ws=new WebSocket("wss://wscpp-andynovo.c9users.io:8081"); | |
ws.onmessage=function(evt){ document.querySelector("#msgs").innerHTML += `<li>${evt.data}</li>`;}; | |
ws.onopen=function(evt){ | |
ws.send("Hello"); | |
} | |
document.getElementById("send").addEventListener("click", function(){ | |
let msg = document.getElementById("msg").value; | |
ws.send(msg); | |
document.getElementById("msg").value = ""; | |
}); | |
} | |
window.onclose=function(){ | |
ws.close(); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment