Skip to content

Instantly share code, notes, and snippets.

@theJenix
Created November 8, 2017 00:51
Show Gist options
  • Save theJenix/1b4ba1e03b48647f7f3b261227be8d4c to your computer and use it in GitHub Desktop.
Save theJenix/1b4ba1e03b48647f7f3b261227be8d4c to your computer and use it in GitHub Desktop.
Test websockets client. Accepts query parameters to specify port and path
<!DOCTYPE html>
<html>
<head>
<title>WebSocket demo</title>
</head>
<body>
<script>
var url = new URL(window.location);
var c = url.searchParams.get("c");
var p = url.searchParams.get("p");
console.log(c);
var ws = new WebSocket("ws://127.0.0.1:" + p + "/" + c),
messages = document.createElement('ul');
ws.onmessage = function (event) {
var messages = document.getElementsByTagName('ul')[0],
message = document.createElement('li'),
content = document.createTextNode(event.data);
message.appendChild(content);
messages.appendChild(message);
};
document.body.appendChild(messages);
ws.onopen = function(event) {
ws.send(JSON.stringify({action: "close", data: "" }));
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment