websocket-sharp/Example3/Public/Js/echotest.js

68 lines
1.3 KiB
JavaScript
Raw Normal View History

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";
2015-07-14 16:20:15 +08:00
//var url = "wss://localhost:5963/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
2015-11-28 14:52:44 +08:00
websocket.onopen = function (e) {
onOpen (e);
2012-09-10 00:36:22 +08:00
};
2015-11-28 14:52:44 +08:00
websocket.onmessage = function (e) {
onMessage (e);
2012-09-10 00:36:22 +08:00
};
2015-11-28 14:52:44 +08:00
websocket.onerror = function (e) {
onError (e);
2012-09-10 00:36:22 +08:00
};
2015-11-28 14:52:44 +08:00
websocket.onclose = function (e) {
onClose (e);
2012-09-10 00:36:22 +08:00
};
}
2015-11-28 14:52:44 +08:00
function onOpen (event) {
2014-10-04 13:50:11 +08:00
writeToScreen ("CONNECTED");
send ("WebSocket rocks");
2012-09-10 00:36:22 +08:00
}
2015-11-28 14:52:44 +08:00
function onMessage (event) {
writeToScreen ('<span style="color: blue;">RESPONSE: ' + event.data + '</span>');
websocket.close ();
2012-09-10 00:36:22 +08:00
}
2015-11-28 14:52:44 +08:00
function onError (event) {
writeToScreen ('<span style="color: red;">ERROR: ' + event.data + '</span>');
2012-09-10 00:36:22 +08:00
}
2015-11-28 14:52:44 +08:00
function onClose (event) {
writeToScreen ("DISCONNECTED");
2012-09-10 00:36:22 +08:00
}
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);