Skip to content

Commit

Permalink
Add websocket test page
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Oct 20, 2016
1 parent 66f2c4c commit eba3662
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ debug-routing: true
routing:
localhost/empty.gif: empty-gif
localhost/sources: src
localhost/websocket.html: websocket-echo-static
localhost/websocket-echo: websocket-echo
chat.example.com/: example-chat
chat.example.com/css: example-chat-static
Expand All @@ -29,6 +30,10 @@ handlers:
destination: superman/

empty-gif: !EmptyGif
websocket-echo-static: !Static
mode: relative_to_domain_root
path: /work/public
text-charset: utf-8
websocket-echo: !WebsocketEcho

src: !Static
Expand Down
63 changes: 63 additions & 0 deletions public/websocket.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
var output;
var input;

function init() {
output = document.getElementById("output");
input = document.getElementById("input");
testWebSocket();
document.getElementById("sendbtn").addEventListener('click',
function(ev) {
doSend(input.value);
}, false);
}

function testWebSocket() {
websocket = new WebSocket("ws://" + location.host +
"/websocket-echo")
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("CONNECTED");
doSend("WebSocket rocks");
}

function onClose(evt) {
writeToScreen("DISCONNECTED");
}

function onMessage(evt) {
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
}

function onError(evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}

function doSend(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>
<h2>WebSocket Test</h2>
<input type="text" id="input">
<input type="button" id="sendbtn" value="SEND">
<div id="output"></div>
</html>

0 comments on commit eba3662

Please sign in to comment.