Skip to content

Instantly share code, notes, and snippets.

@AndyNovo
Created December 4, 2017 17:59
Show Gist options
  • Save AndyNovo/ae62899433e30d17ae731d2d575d334c to your computer and use it in GitHub Desktop.
Save AndyNovo/ae62899433e30d17ae731d2d575d334c to your computer and use it in GitHub Desktop.
<!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