mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-07 21:51:23 +00:00
Migrating to traditional JS handlers.
This commit is contained in:
@@ -18,6 +18,8 @@
|
|||||||
|
|
||||||
function GuacamoleClient(display, tunnel) {
|
function GuacamoleClient(display, tunnel) {
|
||||||
|
|
||||||
|
var guac_client = this;
|
||||||
|
|
||||||
var STATE_IDLE = 0;
|
var STATE_IDLE = 0;
|
||||||
var STATE_CONNECTING = 1;
|
var STATE_CONNECTING = 1;
|
||||||
var STATE_WAITING = 2;
|
var STATE_WAITING = 2;
|
||||||
@@ -26,9 +28,8 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
var STATE_DISCONNECTED = 5;
|
var STATE_DISCONNECTED = 5;
|
||||||
|
|
||||||
var currentState = STATE_IDLE;
|
var currentState = STATE_IDLE;
|
||||||
var stateChangeHandler = null;
|
|
||||||
|
|
||||||
tunnel.setInstructionHandler(doInstruction);
|
tunnel.oninstruction = doInstruction;
|
||||||
|
|
||||||
// Display must be relatively positioned for mouse to be handled properly
|
// Display must be relatively positioned for mouse to be handled properly
|
||||||
display.style.position = "relative";
|
display.style.position = "relative";
|
||||||
@@ -36,15 +37,11 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
function setState(state) {
|
function setState(state) {
|
||||||
if (state != currentState) {
|
if (state != currentState) {
|
||||||
currentState = state;
|
currentState = state;
|
||||||
if (stateChangeHandler)
|
if (guac_client.onstatechange)
|
||||||
stateChangeHandler(currentState);
|
guac_client.onstatechange(currentState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setOnStateChangeHandler = function(handler) {
|
|
||||||
stateChangeHandler = handler;
|
|
||||||
};
|
|
||||||
|
|
||||||
function isConnected() {
|
function isConnected() {
|
||||||
return currentState == STATE_CONNECTED
|
return currentState == STATE_CONNECTED
|
||||||
|| currentState == STATE_WAITING;
|
|| currentState == STATE_WAITING;
|
||||||
@@ -82,7 +79,7 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
cursor.drawImage(cursorRectX, cursorRectY, cursorImage);
|
cursor.drawImage(cursorRectX, cursorRectY, cursorImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.sendKeyEvent = function(pressed, keysym) {
|
guac_client.sendKeyEvent = function(pressed, keysym) {
|
||||||
// Do not send requests if not connected
|
// Do not send requests if not connected
|
||||||
if (!isConnected())
|
if (!isConnected())
|
||||||
return;
|
return;
|
||||||
@@ -90,7 +87,7 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
tunnel.sendMessage("key:" + keysym + "," + pressed + ";");
|
tunnel.sendMessage("key:" + keysym + "," + pressed + ";");
|
||||||
};
|
};
|
||||||
|
|
||||||
this.sendMouseState = function(mouseState) {
|
guac_client.sendMouseState = function(mouseState) {
|
||||||
|
|
||||||
// Do not send requests if not connected
|
// Do not send requests if not connected
|
||||||
if (!isConnected())
|
if (!isConnected())
|
||||||
@@ -116,7 +113,7 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
tunnel.sendMessage("mouse:" + mouseState.getX() + "," + mouseState.getY() + "," + buttonMask + ";");
|
tunnel.sendMessage("mouse:" + mouseState.getX() + "," + mouseState.getY() + "," + buttonMask + ";");
|
||||||
};
|
};
|
||||||
|
|
||||||
this.setClipboard = function(data) {
|
guac_client.setClipboard = function(data) {
|
||||||
|
|
||||||
// Do not send requests if not connected
|
// Do not send requests if not connected
|
||||||
if (!isConnected())
|
if (!isConnected())
|
||||||
@@ -126,21 +123,10 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Handlers
|
// Handlers
|
||||||
|
guac_client.onstatechange = null;
|
||||||
var nameHandler = null;
|
guac_client.onname = null;
|
||||||
this.setNameHandler = function(handler) {
|
guac_client.onerror = null;
|
||||||
nameHandler = handler;
|
guac_client.onclipboard = null;
|
||||||
};
|
|
||||||
|
|
||||||
var errorHandler = null;
|
|
||||||
this.setErrorHandler = function(handler) {
|
|
||||||
errorHandler = handler;
|
|
||||||
};
|
|
||||||
|
|
||||||
var clipboardHandler = null;
|
|
||||||
this.setClipboardHandler = function(handler) {
|
|
||||||
clipboardHandler = handler;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Layers
|
// Layers
|
||||||
var displayWidth = 0;
|
var displayWidth = 0;
|
||||||
@@ -150,7 +136,7 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
var buffers = new Array();
|
var buffers = new Array();
|
||||||
var cursor = null;
|
var cursor = null;
|
||||||
|
|
||||||
this.getLayers = function() {
|
guac_client.getLayers = function() {
|
||||||
return layers;
|
return layers;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -216,16 +202,16 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
var instructionHandlers = {
|
var instructionHandlers = {
|
||||||
|
|
||||||
"error": function(parameters) {
|
"error": function(parameters) {
|
||||||
if (errorHandler) errorHandler(unescapeGuacamoleString(parameters[0]));
|
if (guac_client.onerror) guac_client.onerror(unescapeGuacamoleString(parameters[0]));
|
||||||
disconnect();
|
disconnect();
|
||||||
},
|
},
|
||||||
|
|
||||||
"name": function(parameters) {
|
"name": function(parameters) {
|
||||||
if (nameHandler) nameHandler(unescapeGuacamoleString(parameters[0]));
|
if (guac_client.onname) guac_client.onname(unescapeGuacamoleString(parameters[0]));
|
||||||
},
|
},
|
||||||
|
|
||||||
"clipboard": function(parameters) {
|
"clipboard": function(parameters) {
|
||||||
if (clipboardHandler) clipboardHandler(unescapeGuacamoleString(parameters[0]));
|
if (guac_client.onclipboard) guac_client.onclipboard(unescapeGuacamoleString(parameters[0]));
|
||||||
},
|
},
|
||||||
|
|
||||||
"size": function(parameters) {
|
"size": function(parameters) {
|
||||||
@@ -432,8 +418,8 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.disconnect = disconnect;
|
guac_client.disconnect = disconnect;
|
||||||
this.connect = function(data) {
|
guac_client.connect = function(data) {
|
||||||
|
|
||||||
setState(STATE_CONNECTING);
|
setState(STATE_CONNECTING);
|
||||||
|
|
||||||
@@ -448,7 +434,7 @@ function GuacamoleClient(display, tunnel) {
|
|||||||
setState(STATE_WAITING);
|
setState(STATE_WAITING);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.escapeGuacamoleString = escapeGuacamoleString;
|
guac_client.escapeGuacamoleString = escapeGuacamoleString;
|
||||||
this.unescapeGuacamoleString = unescapeGuacamoleString;
|
guac_client.unescapeGuacamoleString = unescapeGuacamoleString;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,8 @@
|
|||||||
|
|
||||||
function GuacamoleHTTPTunnel(tunnelURL) {
|
function GuacamoleHTTPTunnel(tunnelURL) {
|
||||||
|
|
||||||
|
var tunnel = this;
|
||||||
|
|
||||||
var tunnel_uuid;
|
var tunnel_uuid;
|
||||||
|
|
||||||
var TUNNEL_CONNECT = tunnelURL + "?connect";
|
var TUNNEL_CONNECT = tunnelURL + "?connect";
|
||||||
@@ -36,11 +38,13 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
// Default to polling - will be turned off automatically if not needed
|
// Default to polling - will be turned off automatically if not needed
|
||||||
var pollingMode = POLLING_ENABLED;
|
var pollingMode = POLLING_ENABLED;
|
||||||
|
|
||||||
var instructionHandler = null;
|
|
||||||
|
|
||||||
var sendingMessages = 0;
|
var sendingMessages = 0;
|
||||||
var outputMessageBuffer = "";
|
var outputMessageBuffer = "";
|
||||||
|
|
||||||
|
// Handlers
|
||||||
|
tunnel.onerror = null;
|
||||||
|
tunnel.oninstruction = null;
|
||||||
|
|
||||||
function sendMessage(message) {
|
function sendMessage(message) {
|
||||||
|
|
||||||
// Do not attempt to send messages if not connected
|
// Do not attempt to send messages if not connected
|
||||||
@@ -119,6 +123,16 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
|
|
||||||
// Halt on error during request
|
// Halt on error during request
|
||||||
if (xmlhttprequest.status == 0 || xmlhttprequest.status != 200) {
|
if (xmlhttprequest.status == 0 || xmlhttprequest.status != 200) {
|
||||||
|
|
||||||
|
// Get error message (if any)
|
||||||
|
var message = xmlhttprequest.getResponseHeader("X-Guacamole-Error-Message");
|
||||||
|
if (message)
|
||||||
|
message = "Internal server error";
|
||||||
|
|
||||||
|
// Call error handler
|
||||||
|
if (tunnel.onerror) tunnel.onerror(message);
|
||||||
|
|
||||||
|
// Finish
|
||||||
disconnect();
|
disconnect();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -160,8 +174,8 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Call instruction handler.
|
// Call instruction handler.
|
||||||
if (instructionHandler != null)
|
if (tunnel.oninstruction != null)
|
||||||
instructionHandler(opcode, parameters);
|
tunnel.oninstruction(opcode, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start search at end of string.
|
// Start search at end of string.
|
||||||
@@ -246,11 +260,8 @@ function GuacamoleHTTPTunnel(tunnelURL) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// External API
|
// External API
|
||||||
this.connect = connect;
|
tunnel.connect = connect;
|
||||||
this.disconnect = disconnect;
|
tunnel.disconnect = disconnect;
|
||||||
this.sendMessage = sendMessage;
|
tunnel.sendMessage = sendMessage;
|
||||||
this.setInstructionHandler = function(handler) {
|
|
||||||
instructionHandler = handler;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user