88b71ce171
- Add initial IETF-07 (HyBi-07) protocol version support. This version still uses base64 encoding since the API for binary support is not yet finalized. - Move socket send and recieve functions into the WebSocketServer class instead of having the sub-class do this. This simplifies sub-classes somewhat. The send_frame routine now returns the number of frames that were unable to be sent. If this value is non-zero then the sub-class should call again when the socket is ready until the pending frames count is 0. - Do traffic reporting in the main class instead. - When the client is HyBi style (i.e. IETF-07) then use the sub-protocol header to select whether to do base64 encoding or simply send the frame data raw (binary). Update include/websock.js to send a 'base64' protocol selector. Once the API support binary, then the client will need to detect this and set the protocol to 'binary'.
177 lines
5.4 KiB
HTML
177 lines
5.4 KiB
HTML
<html>
|
|
|
|
<head>
|
|
<title>WebSockets Echo Test</title>
|
|
<script src="include/base64.js"></script>
|
|
<script src="include/util.js"></script>
|
|
<script src="include/webutil.js"></script>
|
|
<!-- Uncomment to activate firebug lite -->
|
|
<!--
|
|
<script type='text/javascript'
|
|
src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
|
|
-->
|
|
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
Host: <input id='host' style='width:100'>
|
|
Port: <input id='port' style='width:50'>
|
|
Encrypt: <input id='encrypt' type='checkbox'>
|
|
<input id='connectButton' type='button' value='Start' style='width:100px'
|
|
onclick="connect();">
|
|
|
|
|
|
<br>
|
|
Log:<br>
|
|
<textarea id="messages" style="font-size: 9;" cols=80 rows=25></textarea>
|
|
</body>
|
|
|
|
|
|
<script>
|
|
var ws, host = null, port = null,
|
|
msg_cnt = 0, send_cnt = 1, echoDelay = 500,
|
|
echo_ref;
|
|
|
|
function message(str) {
|
|
console.log(str);
|
|
cell = $D('messages');
|
|
cell.innerHTML += msg_cnt + ": " + str + "\n";
|
|
cell.scrollTop = cell.scrollHeight;
|
|
msg_cnt++;
|
|
}
|
|
|
|
Array.prototype.pushStr = function (str) {
|
|
var n = str.length;
|
|
for (var i=0; i < n; i++) {
|
|
this.push(str.charCodeAt(i));
|
|
}
|
|
}
|
|
|
|
function send_msg() {
|
|
if (ws.bufferedAmount > 0) {
|
|
console.log("Delaying send");
|
|
return;
|
|
}
|
|
var str = "Message #" + send_cnt, arr = [];
|
|
arr.pushStr(str)
|
|
ws.send(Base64.encode(arr));
|
|
message("Sent message: '" + str + "'");
|
|
send_cnt++;
|
|
}
|
|
|
|
function update_stats() {
|
|
$D('sent').innerHTML = sent;
|
|
$D('received').innerHTML = received;
|
|
$D('errors').innerHTML = errors;
|
|
}
|
|
|
|
function init_ws() {
|
|
console.log(">> init_ws");
|
|
console.log("<< init_ws");
|
|
}
|
|
|
|
function connect() {
|
|
var host = $D('host').value,
|
|
port = $D('port').value,
|
|
scheme = "ws://", uri;
|
|
|
|
console.log(">> connect");
|
|
if ((!host) || (!port)) {
|
|
console.log("must set host and port");
|
|
return;
|
|
}
|
|
|
|
if (ws) {
|
|
ws.close();
|
|
}
|
|
|
|
if ($D('encrypt').checked) {
|
|
scheme = "wss://";
|
|
}
|
|
uri = scheme + host + ":" + port;
|
|
message("connecting to " + uri);
|
|
ws = new WebSocket(uri, "base64");
|
|
|
|
ws.onmessage = function(e) {
|
|
//console.log(">> WebSockets.onmessage");
|
|
var arr = Base64.decode(e.data), str = "", i;
|
|
|
|
for (i = 0; i < arr.length; i++) {
|
|
str = str + String.fromCharCode(arr[i]);
|
|
}
|
|
|
|
message("Received message '" + str + "'");
|
|
//console.log("<< WebSockets.onmessage");
|
|
};
|
|
ws.onopen = function(e) {
|
|
console.log(">> WebSockets.onopen");
|
|
echo_ref = setInterval(send_msg, echoDelay);
|
|
console.log("<< WebSockets.onopen");
|
|
};
|
|
ws.onclose = function(e) {
|
|
console.log(">> WebSockets.onclose");
|
|
if (echo_ref) {
|
|
clearInterval(echo_ref);
|
|
echo_ref = null;
|
|
}
|
|
console.log("<< WebSockets.onclose");
|
|
};
|
|
ws.onerror = function(e) {
|
|
console.log(">> WebSockets.onerror");
|
|
if (echo_ref) {
|
|
clearInterval(echo_ref);
|
|
echo_ref = null;
|
|
}
|
|
console.log("<< WebSockets.onerror");
|
|
};
|
|
|
|
$D('connectButton').value = "Stop";
|
|
$D('connectButton').onclick = disconnect;
|
|
console.log("<< connect");
|
|
}
|
|
|
|
function disconnect() {
|
|
console.log(">> disconnect");
|
|
if (ws) {
|
|
ws.close();
|
|
}
|
|
|
|
if (echo_ref) {
|
|
clearInterval(echo_ref);
|
|
}
|
|
|
|
$D('connectButton').value = "Start";
|
|
$D('connectButton').onclick = connect;
|
|
console.log("<< disconnect");
|
|
}
|
|
|
|
|
|
/* If no builtin websockets then load web_socket.js */
|
|
if (window.WebSocket) {
|
|
VNC_native_ws = true;
|
|
} else {
|
|
VNC_native_ws = false;
|
|
console.log("Loading web-socket-js flash bridge");
|
|
var extra = "<script src='include/web-socket-js/swfobject.js'><\/script>";
|
|
extra += "<script src='include/web-socket-js/FABridge.js'><\/script>";
|
|
extra += "<script src='include/web-socket-js/web_socket.js'><\/script>";
|
|
document.write(extra);
|
|
}
|
|
|
|
window.onload = function() {
|
|
console.log("onload");
|
|
if (!VNC_native_ws) {
|
|
console.log("initializing web-socket-js flash bridge");
|
|
WebSocket.__swfLocation = "include/web-socket-js/WebSocketMain.swf";
|
|
WebSocket.__initialize();
|
|
}
|
|
var url = document.location.href;
|
|
$D('host').value = (url.match(/host=([^&#]*)/) || ['',''])[1];
|
|
$D('port').value = (url.match(/port=([^&#]*)/) || ['',''])[1];
|
|
}
|
|
</script>
|
|
|
|
</html>
|