Created
November 8, 2017 00:51
-
-
Save theJenix/1b4ba1e03b48647f7f3b261227be8d4c to your computer and use it in GitHub Desktop.
Test websockets client. Accepts query parameters to specify port and path
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>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