<title>WebSocket Test</title>
<h2>WebSocket Test</h2>
<div id="output"></div>
</html>© 2013 Kaazing Corporation. All Rights Reserved - Contact Webmaster
// the only modification that I've made to this code is to add the writeToScreen function into every step - Troy Whorten.
// code from http://www.websocket.org/echo.html
var wsUri = "ws://echo.websocket.org/";
var output;
function init() {
//writeToScreen("called init()");
output = document.getElementById("output");
writeToScreen("called init()");
testWebSocket();
}
function testWebSocket() {
writeToScreen("called testWebSocket()");
websocket = new WebSocket(wsUri);
websocket.onopen = function (evt) {
onOpen(evt)
};
websocket.onclose = function (evt) {
onClose(evt)
};
websocket.onmessage = function (evt) {
onMessage(evt)
};
websocket.onerror = function (evt) {
onError(evt)
};
}
function onOpen(evt) {
writeToScreen("called onOpen()");
writeToScreen("CONNECTED");
doSend("WebSocket rocks");
}
function onClose(evt) {
writeToScreen("called onClose()");
writeToScreen("DISCONNECTED");
}
function onMessage(evt) {
writeToScreen("called onMessage()");
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
websocket.close();
}
function onError(evt) {
writeToScreen("called onError()");
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doSend(message) {
writeToScreen("called doSend()");
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);