Created
September 28, 2017 05:38
-
-
Save ncole458/6fc9a656dcf52bc3881513c62970281f to your computer and use it in GitHub Desktop.
WebSockets Testing
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 lang="en" dir="ltr" class="no-js"> | |
<head> | |
<title>WebSockets</title> | |
<script language="javascript" type="text/javascript"> | |
var wsUri = "wss://url.com.au/websockets/"; | |
var output; | |
function init() | |
{ | |
output = document.getElementById("output"); | |
testWebSocket(); | |
} | |
function testWebSocket() | |
{ | |
websockets = new WebSocket(wsUri); | |
websockets.onopen = function(evt) { onOpen(evt) }; | |
websockets.onclose = function(evt) { onClose(evt) }; | |
websockets.onmessage = function(evt) { onMessage(evt) }; | |
websockets.onerror = function(evt) { onError(evt) }; | |
} | |
function onOpen(evt) | |
{ | |
writeToScreen("CONNECTED"); | |
doSend("WebSocket rocks"); | |
} | |
function onClose(evt) | |
{ | |
writeToScreen("DISCONNECTED"); | |
} | |
function onMessage(evt) | |
{ | |
writeToScreen('RESPONSE: ' + evt.data+''); | |
websockets.close(); | |
} | |
function onError(evt) | |
{ | |
writeToScreen('ERROR: ' + evt.data); | |
} | |
function doSend(message) | |
{ | |
writeToScreen("SENT: " + message); | |
websockets.send(message); | |
} | |
function writeToScreen(message) | |
{ | |
var pre = document.createElement("p"); | |
pre.style.wordWrap = "break-word"; | |
pre.innerHTML = message; | |
output.appendChild(pre); | |
} | |
window.addEventListener("load", init, false); | |
</script> | |
</head> | |
<body> | |
<h1>WebSockets</h1> | |
<div id="output"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment