2014-10-04 13:50:11 +08:00
|
|
|
/*
|
2012-09-10 00:36:22 +08:00
|
|
|
* echotest.js
|
2014-10-04 13:50:11 +08:00
|
|
|
*
|
|
|
|
* Derived from Echo Test of WebSocket.org (http://www.websocket.org/echo.html).
|
2012-09-10 00:36:22 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Kaazing Corporation.
|
|
|
|
*/
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
var url = "ws://localhost:4649/Echo";
|
|
|
|
//var url = "wss://localhost:4649/Echo";
|
2012-09-10 00:36:22 +08:00
|
|
|
var output;
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
function init () {
|
|
|
|
output = document.getElementById ("output");
|
|
|
|
doWebSocket ();
|
2012-09-10 00:36:22 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
function doWebSocket () {
|
|
|
|
websocket = new WebSocket (url);
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
websocket.onopen = function (evt) {
|
|
|
|
onOpen (evt)
|
2012-09-10 00:36:22 +08:00
|
|
|
};
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
websocket.onclose = function (evt) {
|
|
|
|
onClose (evt)
|
2012-09-10 00:36:22 +08:00
|
|
|
};
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
websocket.onmessage = function (evt) {
|
|
|
|
onMessage (evt)
|
2012-09-10 00:36:22 +08:00
|
|
|
};
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
websocket.onerror = function (evt) {
|
|
|
|
onError (evt)
|
2012-09-10 00:36:22 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
function onOpen (evt) {
|
|
|
|
writeToScreen ("CONNECTED");
|
|
|
|
send ("WebSocket rocks");
|
2012-09-10 00:36:22 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
function onClose (evt) {
|
|
|
|
writeToScreen ("DISCONNECTED");
|
2012-09-10 00:36:22 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
function onMessage (evt) {
|
|
|
|
writeToScreen ('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
|
|
|
|
websocket.close ();
|
2012-09-10 00:36:22 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
function onError (evt) {
|
2012-09-10 00:36:22 +08:00
|
|
|
writeToScreen('<span style="color: red;">ERROR: ' + evt.data + '</span>');
|
|
|
|
}
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
function send (message) {
|
|
|
|
writeToScreen ("SENT: " + message);
|
|
|
|
websocket.send (message);
|
2012-09-10 00:36:22 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
function writeToScreen (message) {
|
|
|
|
var pre = document.createElement ("p");
|
2012-09-10 00:36:22 +08:00
|
|
|
pre.style.wordWrap = "break-word";
|
|
|
|
pre.innerHTML = message;
|
2014-10-04 13:50:11 +08:00
|
|
|
output.appendChild (pre);
|
2012-09-10 00:36:22 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 13:50:11 +08:00
|
|
|
window.addEventListener ("load", init, false);
|