Created
May 11, 2018 04:15
-
-
Save fake-name/df056d21e155b07a37814b5228834849 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> | |
<head> | |
<meta charset="utf-8" /> | |
<title>WebSocket Test</title> | |
</head> | |
<body> | |
<h2>WebSocket Test</h2> | |
<div id="output"></div> | |
<script language="javascript" type="text/javascript"> | |
'use strict'; | |
var wsUri = "wss://echo.websocket.org/"; | |
var output; | |
var websocket; | |
function init() | |
{ | |
output = document.getElementById("output"); | |
testWebSocket(); | |
} | |
function testWebSocket() | |
{ | |
websocket = new WebSocket(wsUri); | |
websocket.onopen = onOpen; | |
websocket.onclose = onClose; | |
websocket.onmessage = onMessage; | |
websocket.onerror = onError; | |
} | |
function onOpen(evt) | |
{ | |
console.log("onOpen") | |
writeToScreen("CONNECTED"); | |
doSend("WebSocket rocks"); | |
} | |
function onClose(evt) | |
{ | |
console.log("onClose") | |
writeToScreen("DISCONNECTED"); | |
} | |
function onMessage(evt) | |
{ | |
console.log("onMessage - ", 'RESPONSE: ' + evt.data) | |
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>'); | |
websocket.close(); | |
} | |
function onError(evt) | |
{ | |
console.log("onError") | |
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data); | |
} | |
function doSend(message) | |
{ | |
console.log("doSend - ", "SENT: " + message) | |
writeToScreen("SENT: " + message); | |
websocket.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> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment