Skip to content
Find file
442f693 Sep 2, 2013
97 lines (79 sloc) 2.14 KB
<html>
<head>
<title>WebSocket Tester</title>
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script language="javascript" type="text/javascript">
var websocket;
var ping;
jQuery(function ($) {
$('#connect').click(doConnect);
$('#disconnect').click(doDisconnect);
$('#send').click(function () {
doSend( $('#message').val() )
});
function doConnect() {
websocket = new WebSocket( $("#target").val() );
websocket.onopen = function (evt) {
onOpen(evt)
var ping = setInterval(function () { doPing() }, 1000);
};
websocket.onclose = function (evt) {
onClose(evt)
};
websocket.onmessage = function (evt) {
onMessage(evt)
};
websocket.onerror = function (evt) {
onError(evt)
};
}
function doDisconnect() {
websocket.close();
}
function onOpen(evt) {
writeToScreen("CONNECTED");
}
function onClose(evt) {
writeToScreen("DISCONNECTED");
}
function onMessage(evt) {
if ((evt.data != "ping") && (evt.data != "pong")) {
writeToScreen('RECIEVED: ' + evt.data);
}
}
function onError(evt) {
writeToScreen('ERROR:' + evt.data);
}
function doSend(message) {
if (message != "ping") {
writeToScreen('SENT: ' + message);
}
websocket.send(message);
}
function writeToScreen(message) {
$('#output').append(message + '<br /><br />');
}
function doPing() {
if (websocket != "undefined") {
doSend("ping");
}
}
});
</script>
</head>
<body>
<h2>WebSocket Tester</h2>
Target:
<input type="text" id="target" value="" />
<br />
<button id="connect">Connect</button>
<button id="disconnect">Disconnect</button>
<br />
<br />Message:
<input type="text" id="message" value="" />
<button id="send">Send</button>
<br />
<br />Output:
<br /> <pre><div id="output"></div></pre>
</body>
</html>
Something went wrong with that request. Please try again.