GUAC-605: Fix usage of scope within client and tunnel.

This commit is contained in:
Michael Jumper
2014-11-16 15:41:06 -08:00
parent 0bd1fdc16f
commit 4e67487077
3 changed files with 102 additions and 95 deletions

View File

@@ -312,7 +312,7 @@ angular.module('client').directive('guacClient', [function guacClient() {
// Get new client instance // Get new client instance
var tunnel = guacTunnelFactory.getInstance($scope); var tunnel = guacTunnelFactory.getInstance($scope);
client = guacClientFactory.getInstance(tunnel, $scope); client = guacClientFactory.getInstance($scope, tunnel);
// Init display // Init display
display = client.getDisplay(); display = client.getDisplay();

View File

@@ -32,15 +32,11 @@ angular.module('client').factory('guacClientFactory', ['$rootScope',
* Returns a new Guacamole client instance which connects using the * Returns a new Guacamole client instance which connects using the
* provided tunnel. * provided tunnel.
* *
* @param {Scope} $scope The current scope.
* @param {Guacamole.Tunnel} tunnel The tunnel to connect through. * @param {Guacamole.Tunnel} tunnel The tunnel to connect through.
* @param {Scope} [$scope] The current scope. If ommitted, the root scope
* will be used.
* @returns {Guacamole.Client} A new Guacamole client instance. * @returns {Guacamole.Client} A new Guacamole client instance.
*/ */
service.getInstance = function getClientInstance(tunnel, $scope) { service.getInstance = function getClientInstance($scope, tunnel) {
// Use root scope if no other scope provided
$scope = $scope || $rootScope;
// Instantiate client // Instantiate client
var guacClient = new Guacamole.Client(tunnel); var guacClient = new Guacamole.Client(tunnel);
@@ -49,31 +45,32 @@ angular.module('client').factory('guacClientFactory', ['$rootScope',
* Fire guacClientStateChange events when client state changes. * Fire guacClientStateChange events when client state changes.
*/ */
guacClient.onstatechange = function onClientStateChange(clientState) { guacClient.onstatechange = function onClientStateChange(clientState) {
$scope.safeApply(function() {
switch (clientState) { switch (clientState) {
// Idle // Idle
case 0: case 0:
$rootScope.$broadcast('guacClientStateChange', guacClient, "idle"); $scope.$emit('guacClientStateChange', guacClient, "idle");
break; break;
// Connecting // Connecting
case 1: case 1:
$rootScope.$broadcast('guacClientStateChange', guacClient, "connecting"); $scope.$emit('guacClientStateChange', guacClient, "connecting");
break; break;
// Connected + waiting // Connected + waiting
case 2: case 2:
$rootScope.$broadcast('guacClientStateChange', guacClient, "waiting"); $scope.$emit('guacClientStateChange', guacClient, "waiting");
break; break;
// Connected // Connected
case 3: case 3:
$rootScope.$broadcast('guacClientStateChange', guacClient, "connected"); $scope.$emit('guacClientStateChange', guacClient, "connected");
// Update server clipboard with current data // Update server clipboard with current data
if ($rootScope.clipboard) if ($scope.clipboard)
guacClient.setClipboard($rootScope.clipboard); guacClient.setClipboard($scope.clipboard);
break; break;
@@ -83,13 +80,17 @@ angular.module('client').factory('guacClientFactory', ['$rootScope',
break; break;
} }
});
}; };
/* /*
* Fire guacClientName events when a new name is received. * Fire guacClientName events when a new name is received.
*/ */
guacClient.onname = function onClientName(name) { guacClient.onname = function onClientName(name) {
$rootScope.$broadcast('guacClientName', guacClient, name); $scope.safeApply(function() {
$scope.$emit('guacClientName', guacClient, name);
});
}; };
/* /*
@@ -97,18 +98,21 @@ angular.module('client').factory('guacClientFactory', ['$rootScope',
* error. * error.
*/ */
guacClient.onerror = function onClientError(status) { guacClient.onerror = function onClientError(status) {
$scope.safeApply(function() {
// Disconnect, if connected // Disconnect, if connected
guacClient.disconnect(); guacClient.disconnect();
$rootScope.$broadcast('guacClientError', guacClient, status.code); $scope.$emit('guacClientError', guacClient, status.code);
});
}; };
/* /*
* Fire guacClientClipboard events after new clipboard data is received. * Fire guacClientClipboard events after new clipboard data is received.
*/ */
guacClient.onclipboard = function onClientClipboard(stream, mimetype) { guacClient.onclipboard = function onClientClipboard(stream, mimetype) {
$scope.safeApply(function() {
// Only text/plain is supported for now // Only text/plain is supported for now
if (mimetype !== "text/plain") { if (mimetype !== "text/plain") {
@@ -127,9 +131,10 @@ angular.module('client').factory('guacClientFactory', ['$rootScope',
// Emit event when done // Emit event when done
reader.onend = function clipboard_text_end() { reader.onend = function clipboard_text_end() {
$rootScope.$broadcast('guacClientClipboard', guacClient, data); $scope.$emit('guacClientClipboard', guacClient, data);
}; };
});
}; };
/* /*
@@ -137,22 +142,23 @@ angular.module('client').factory('guacClientFactory', ['$rootScope',
* the receipt of files. * the receipt of files.
*/ */
guacClient.onfile = function onClientFile(stream, mimetype, filename) { guacClient.onfile = function onClientFile(stream, mimetype, filename) {
$scope.safeApply(function() {
// Begin file download // Begin file download
var guacFileStartEvent = $rootScope.$broadcast('guacFileStart', guacClient, stream.index, mimetype, filename); var guacFileStartEvent = $scope.$emit('guacFileStart', guacClient, stream.index, mimetype, filename);
if (!guacFileStartEvent.defaultPrevented) { if (!guacFileStartEvent.defaultPrevented) {
var blob_reader = new Guacamole.BlobReader(stream, mimetype); var blob_reader = new Guacamole.BlobReader(stream, mimetype);
// Update progress as data is received // Update progress as data is received
blob_reader.onprogress = function onprogress() { blob_reader.onprogress = function onprogress() {
$rootScope.$broadcast('guacFileProgress', guacClient, stream.index, mimetype, filename); $scope.$emit('guacFileProgress', guacClient, stream.index, mimetype, filename);
stream.sendAck("Received", Guacamole.Status.Code.SUCCESS); stream.sendAck("Received", Guacamole.Status.Code.SUCCESS);
}; };
// When complete, prompt for download // When complete, prompt for download
blob_reader.onend = function onend() { blob_reader.onend = function onend() {
$rootScope.$broadcast('guacFileEnd', guacClient, stream.index, mimetype, filename); $scope.$emit('guacFileEnd', guacClient, stream.index, mimetype, filename);
}; };
stream.sendAck("Ready", Guacamole.Status.Code.SUCCESS); stream.sendAck("Ready", Guacamole.Status.Code.SUCCESS);
@@ -163,6 +169,7 @@ angular.module('client').factory('guacClientFactory', ['$rootScope',
else else
stream.sendAck("Download canceled", Guacamole.Status.Code.UNSUPPORTED); stream.sendAck("Download canceled", Guacamole.Status.Code.UNSUPPORTED);
});
}; };
return guacClient; return guacClient;

View File

@@ -32,15 +32,11 @@ angular.module('client').factory('guacTunnelFactory', ['$rootScope', '$window',
* Returns a new Guacamole tunnel instance, using an implementation that is * Returns a new Guacamole tunnel instance, using an implementation that is
* supported by the web browser. * supported by the web browser.
* *
* @param {Scope} [$scope] The current scope. If ommitted, the root scope * @param {Scope} $scope The current scope.
* will be used.
* @returns {Guacamole.Tunnel} A new Guacamole tunnel instance. * @returns {Guacamole.Tunnel} A new Guacamole tunnel instance.
*/ */
service.getInstance = function getTunnelInstance($scope) { service.getInstance = function getTunnelInstance($scope) {
// Use root scope if no other scope provided
$scope = $scope || $rootScope;
var tunnel; var tunnel;
// If WebSocket available, try to use it. // If WebSocket available, try to use it.
@@ -56,28 +52,32 @@ angular.module('client').factory('guacTunnelFactory', ['$rootScope', '$window',
// Fire events for tunnel errors // Fire events for tunnel errors
tunnel.onerror = function onTunnelError(status) { tunnel.onerror = function onTunnelError(status) {
$rootScope.$broadcast('guacTunnelError', tunnel, status.code); $scope.safeApply(function() {
$scope.$emit('guacTunnelError', tunnel, status.code);
});
}; };
// Fire events for tunnel state changes // Fire events for tunnel state changes
tunnel.onstatechange = function onTunnelStateChange(state) { tunnel.onstatechange = function onTunnelStateChange(state) {
$scope.safeApply(function() {
switch (state) { switch (state) {
case Guacamole.Tunnel.State.CONNECTING: case Guacamole.Tunnel.State.CONNECTING:
$rootScope.$broadcast('guacTunnelStateChange', tunnel, 'connecting'); $scope.$emit('guacTunnelStateChange', tunnel, 'connecting');
break; break;
case Guacamole.Tunnel.State.OPEN: case Guacamole.Tunnel.State.OPEN:
$rootScope.$broadcast('guacTunnelStateChange', tunnel, 'open'); $scope.$emit('guacTunnelStateChange', tunnel, 'open');
break; break;
case Guacamole.Tunnel.State.CLOSED: case Guacamole.Tunnel.State.CLOSED:
$rootScope.$broadcast('guacTunnelStateChange', tunnel, 'closed'); $scope.$emit('guacTunnelStateChange', tunnel, 'closed');
break; break;
} }
});
}; };
return tunnel; return tunnel;