mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-06 21:27:40 +00:00
Added missing semicolons, improved state handling
This commit is contained in:
@@ -43,7 +43,7 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
|
|
||||||
this.setOnStateChangeHandler = function(handler) {
|
this.setOnStateChangeHandler = function(handler) {
|
||||||
stateChangeHandler = handler;
|
stateChangeHandler = handler;
|
||||||
}
|
};
|
||||||
|
|
||||||
function isConnected() {
|
function isConnected() {
|
||||||
return currentState == STATE_CONNECTED
|
return currentState == STATE_CONNECTED
|
||||||
@@ -89,7 +89,7 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
tunnel.sendMessage("key:" + keysym + "," + pressed + ";");
|
tunnel.sendMessage("key:" + keysym + "," + pressed + ";");
|
||||||
}
|
};
|
||||||
|
|
||||||
this.sendMouseState = function(mouseState) {
|
this.sendMouseState = function(mouseState) {
|
||||||
|
|
||||||
@@ -115,7 +115,7 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
|
|
||||||
// Send message
|
// Send message
|
||||||
tunnel.sendMessage("mouse:" + mouseState.getX() + "," + mouseState.getY() + "," + buttonMask + ";");
|
tunnel.sendMessage("mouse:" + mouseState.getX() + "," + mouseState.getY() + "," + buttonMask + ";");
|
||||||
}
|
};
|
||||||
|
|
||||||
this.setClipboard = function(data) {
|
this.setClipboard = function(data) {
|
||||||
|
|
||||||
@@ -123,8 +123,8 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
if (!isConnected())
|
if (!isConnected())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
tunnel.sendMessage("clipboard:" + tunnel.escapeGuacamoleString(data) + ";");
|
tunnel.sendMessage("clipboard:" + escapeGuacamoleString(data) + ";");
|
||||||
}
|
};
|
||||||
|
|
||||||
// Handlers
|
// Handlers
|
||||||
|
|
||||||
@@ -153,7 +153,7 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
|
|
||||||
this.getLayers = function() {
|
this.getLayers = function() {
|
||||||
return layers;
|
return layers;
|
||||||
}
|
};
|
||||||
|
|
||||||
function getLayer(index) {
|
function getLayer(index) {
|
||||||
|
|
||||||
@@ -217,16 +217,16 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
var instructionHandlers = {
|
var instructionHandlers = {
|
||||||
|
|
||||||
"error": function(parameters) {
|
"error": function(parameters) {
|
||||||
if (errorHandler) errorHandler(tunnel.unescapeGuacamoleString(parameters[0]));
|
if (errorHandler) errorHandler(unescapeGuacamoleString(parameters[0]));
|
||||||
disconnect();
|
disconnect();
|
||||||
},
|
},
|
||||||
|
|
||||||
"name": function(parameters) {
|
"name": function(parameters) {
|
||||||
if (nameHandler) nameHandler(tunnel.unescapeGuacamoleString(parameters[0]));
|
if (nameHandler) nameHandler(unescapeGuacamoleString(parameters[0]));
|
||||||
},
|
},
|
||||||
|
|
||||||
"clipboard": function(parameters) {
|
"clipboard": function(parameters) {
|
||||||
if (clipboardHandler) clipboardHandler(tunnel.unescapeGuacamoleString(parameters[0]));
|
if (clipboardHandler) clipboardHandler(unescapeGuacamoleString(parameters[0]));
|
||||||
},
|
},
|
||||||
|
|
||||||
"size": function(parameters) {
|
"size": function(parameters) {
|
||||||
@@ -319,17 +319,8 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
handler(parameters);
|
handler(parameters);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
this.connect = function() {
|
|
||||||
|
|
||||||
setState(STATE_CONNECTING);
|
|
||||||
tunnel.connect();
|
|
||||||
setState(STATE_WAITING);
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
function disconnect() {
|
function disconnect() {
|
||||||
|
|
||||||
// Only attempt disconnection not disconnected.
|
// Only attempt disconnection not disconnected.
|
||||||
@@ -344,6 +335,67 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function escapeGuacamoleString(str) {
|
||||||
|
|
||||||
|
var escapedString = "";
|
||||||
|
|
||||||
|
for (var i=0; i<str.length; i++) {
|
||||||
|
|
||||||
|
var c = str.charAt(i);
|
||||||
|
if (c == ",")
|
||||||
|
escapedString += "\\c";
|
||||||
|
else if (c == ";")
|
||||||
|
escapedString += "\\s";
|
||||||
|
else if (c == "\\")
|
||||||
|
escapedString += "\\\\";
|
||||||
|
else
|
||||||
|
escapedString += c;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return escapedString;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function unescapeGuacamoleString(str) {
|
||||||
|
|
||||||
|
var unescapedString = "";
|
||||||
|
|
||||||
|
for (var i=0; i<str.length; i++) {
|
||||||
|
|
||||||
|
var c = str.charAt(i);
|
||||||
|
if (c == "\\" && i<str.length-1) {
|
||||||
|
|
||||||
|
var escapeChar = str.charAt(++i);
|
||||||
|
if (escapeChar == "c")
|
||||||
|
unescapedString += ",";
|
||||||
|
else if (escapeChar == "s")
|
||||||
|
unescapedString += ";";
|
||||||
|
else if (escapeChar == "\\")
|
||||||
|
unescapedString += "\\";
|
||||||
|
else
|
||||||
|
unescapedString += "\\" + escapeChar;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
unescapedString += c;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return unescapedString;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
this.disconnect = disconnect;
|
this.disconnect = disconnect;
|
||||||
|
this.connect = function() {
|
||||||
|
|
||||||
|
setState(STATE_CONNECTING);
|
||||||
|
tunnel.connect();
|
||||||
|
setState(STATE_WAITING);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
this.escapeGuacamoleString = escapeGuacamoleString;
|
||||||
|
this.unescapeGuacamoleString = unescapeGuacamoleString;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -22,8 +22,18 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
var TUNNEL_READ = tunnelURL + "?read";
|
var TUNNEL_READ = tunnelURL + "?read";
|
||||||
var TUNNEL_WRITE = tunnelURL + "?write";
|
var TUNNEL_WRITE = tunnelURL + "?write";
|
||||||
|
|
||||||
var connected = 0;
|
var STATE_IDLE = 0;
|
||||||
var pollResponse = 1; // Default to polling - will be turned off automatically if not needed
|
var STATE_CONNECTED = 1;
|
||||||
|
var STATE_DISCONNECTED = 2;
|
||||||
|
|
||||||
|
var currentState = STATE_IDLE;
|
||||||
|
|
||||||
|
var POLLING_ENABLED = 1;
|
||||||
|
var POLLING_DISABLED = 0;
|
||||||
|
|
||||||
|
// Default to polling - will be turned off automatically if not needed
|
||||||
|
var pollingMode = POLLING_ENABLED;
|
||||||
|
|
||||||
var instructionHandler = null;
|
var instructionHandler = null;
|
||||||
|
|
||||||
var sendingMessages = 0;
|
var sendingMessages = 0;
|
||||||
@@ -32,7 +42,7 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
function sendMessage(message) {
|
function sendMessage(message) {
|
||||||
|
|
||||||
// Do not attempt to send messages if not connected
|
// Do not attempt to send messages if not connected
|
||||||
if (!connected)
|
if (currentState != STATE_CONNECTED)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Add event to queue, restart send loop if finished.
|
// Add event to queue, restart send loop if finished.
|
||||||
@@ -40,7 +50,7 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
if (sendingMessages == 0)
|
if (sendingMessages == 0)
|
||||||
sendPendingMessages();
|
sendPendingMessages();
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
function sendPendingMessages() {
|
function sendPendingMessages() {
|
||||||
|
|
||||||
@@ -81,7 +91,7 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
function parseResponse() {
|
function parseResponse() {
|
||||||
|
|
||||||
// Do not handle responses if not connected
|
// Do not handle responses if not connected
|
||||||
if (!connected) {
|
if (currentState != STATE_CONNECTED) {
|
||||||
|
|
||||||
// Clean up interval if polling
|
// Clean up interval if polling
|
||||||
if (interval != null)
|
if (interval != null)
|
||||||
@@ -99,7 +109,7 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
xmlhttprequest.readyState == 4) {
|
xmlhttprequest.readyState == 4) {
|
||||||
|
|
||||||
// Also poll every 30ms (some browsers don't repeatedly call onreadystatechange for new data)
|
// Also poll every 30ms (some browsers don't repeatedly call onreadystatechange for new data)
|
||||||
if (pollResponse == 1) {
|
if (pollingMode == POLLING_ENABLED) {
|
||||||
if (xmlhttprequest.readyState == 3 && interval == null)
|
if (xmlhttprequest.readyState == 3 && interval == null)
|
||||||
interval = setInterval(parseResponse, 30);
|
interval = setInterval(parseResponse, 30);
|
||||||
else if (xmlhttprequest.readyState == 4 && interval != null)
|
else if (xmlhttprequest.readyState == 4 && interval != null)
|
||||||
@@ -165,7 +175,7 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
|
|
||||||
// If response polling enabled, attempt to detect if still
|
// If response polling enabled, attempt to detect if still
|
||||||
// necessary (via wrapping parseResponse())
|
// necessary (via wrapping parseResponse())
|
||||||
if (pollResponse == 1) {
|
if (pollingMode == POLLING_ENABLED) {
|
||||||
xmlhttprequest.onreadystatechange = function() {
|
xmlhttprequest.onreadystatechange = function() {
|
||||||
|
|
||||||
// If we receive two or more readyState==3 events,
|
// If we receive two or more readyState==3 events,
|
||||||
@@ -173,7 +183,7 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
if (xmlhttprequest.readyState == 3) {
|
if (xmlhttprequest.readyState == 3) {
|
||||||
dataUpdateEvents++;
|
dataUpdateEvents++;
|
||||||
if (dataUpdateEvents >= 2) {
|
if (dataUpdateEvents >= 2) {
|
||||||
pollResponse = 0;
|
pollingMode = POLLING_DISABLED;
|
||||||
xmlhttprequest.onreadystatechange = parseResponse;
|
xmlhttprequest.onreadystatechange = parseResponse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -212,13 +222,13 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
connect_xmlhttprequest.send(null);
|
connect_xmlhttprequest.send(null);
|
||||||
|
|
||||||
// Start reading data
|
// Start reading data
|
||||||
connected = 1;
|
currentState = STATE_CONNECTED;
|
||||||
handleResponse(makeRequest());
|
handleResponse(makeRequest());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function disconnect() {
|
function disconnect() {
|
||||||
connected = 0;
|
currentState = STATE_DISCONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// External API
|
// External API
|
||||||
@@ -227,57 +237,6 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
this.sendMessage = sendMessage;
|
this.sendMessage = sendMessage;
|
||||||
this.setInstructionHandler = function(handler) {
|
this.setInstructionHandler = function(handler) {
|
||||||
instructionHandler = handler;
|
instructionHandler = handler;
|
||||||
}
|
};
|
||||||
|
|
||||||
this.escapeGuacamoleString = function(str) {
|
|
||||||
|
|
||||||
var escapedString = "";
|
|
||||||
|
|
||||||
for (var i=0; i<str.length; i++) {
|
|
||||||
|
|
||||||
var c = str.charAt(i);
|
|
||||||
if (c == ",")
|
|
||||||
escapedString += "\\c";
|
|
||||||
else if (c == ";")
|
|
||||||
escapedString += "\\s";
|
|
||||||
else if (c == "\\")
|
|
||||||
escapedString += "\\\\";
|
|
||||||
else
|
|
||||||
escapedString += c;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return escapedString;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
this.unescapeGuacamoleString = function(str) {
|
|
||||||
|
|
||||||
var unescapedString = "";
|
|
||||||
|
|
||||||
for (var i=0; i<str.length; i++) {
|
|
||||||
|
|
||||||
var c = str.charAt(i);
|
|
||||||
if (c == "\\" && i<str.length-1) {
|
|
||||||
|
|
||||||
var escapeChar = str.charAt(++i);
|
|
||||||
if (escapeChar == "c")
|
|
||||||
unescapedString += ",";
|
|
||||||
else if (escapeChar == "s")
|
|
||||||
unescapedString += ";";
|
|
||||||
else if (escapeChar == "\\")
|
|
||||||
unescapedString += "\\";
|
|
||||||
else
|
|
||||||
unescapedString += "\\" + escapeChar;
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
unescapedString += c;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return unescapedString;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user