/// <summary> /// Initializes the client and establishes a new connection to the Eyeshot Web Service. /// </summary> /// <remarks>The client must support the callback for listening the web service replies.</remarks> private void ClientConnect() { btnConnect.Enabled = false; if (_client != null) { try { _client.Close(); } catch { _client.Abort(); } finally { _client = null; } } _callback = new EyeshotCallback(); CallbackEventSubscription(true); var instanceContext = new InstanceContext(_callback); _client = new EyeshotClient(instanceContext); ClientEventSubscription(true); bool opened = false; try { _client.Open(); opened = true; } catch (Exception ex) { if (ex is PlatformNotSupportedException) { AppendToLog("This platform does not support client side WebSockets natively. Please try with a more recent Windows version (Windows 8 or higher)"); } else { AppendToLog(ex.Message); } btnConnect.Enabled = true; } if (!opened) { return; } _connectionId = _client.Connect(accessKey); if (_connectionId > 0) { lblConnectionId.Text = "Id: " + _connectionId; _downloadFolder = System.IO.Path.Combine(_rootDownloadFolder, String.Format(@"{0}\", _connectionId)); if (!Directory.Exists(_downloadFolder)) { Directory.CreateDirectory(_downloadFolder); } InitReadingSettings(); btnInputFile.Enabled = btnUpdateSettings.Enabled = txtInput.Enabled = btnUpload.Enabled = trackUpload.Enabled = true; btnExtraFiles.Enabled = false; btnDisconnect.Enabled = true; AppendToLog("Connection established.", true); } else { AppendToLog("Connection failed.", true); if (_connectionId == -1) { AppendToLog("Invalid access key. Get it here: https://www.devdept.com/Account"); } else if (_connectionId == -2) { AppendToLog("The access key has been suspended."); } else if (_connectionId == -3) { AppendToLog("Error while opening the connection."); } ClientShutdown(); btnConnect.Enabled = true; } }
/// <summary> /// Disconnect and close the client connection with the Eyeshot Web Service. /// </summary> private void ClientShutdown() { if (_callback != null) { try { CallbackEventSubscription(false); } finally { _callback = null; } } if (_client != null) { try { ClientEventSubscription(false); _client.Disconnect(); _client.Close(); } catch { _client.Abort(); } } if (chkDeleteFiles.Checked) { treePicView.SelectedNode = null; // collects nodes of the current connection... List <TreeNode> removeList = new List <TreeNode>(); foreach (TreeNode node in treePicView.Nodes) { if (node.Tag.ToString().Contains(_downloadFolder)) { removeList.Add(node); } } // ...and remove them foreach (TreeNode node in removeList) { treePicView.Nodes.Remove(node); } // close the visualizer if (_outputVisualizer != null) { _outputVisualizer.Close(); } // finally I can delete the subfolder. try { if (_downloadFolder != null) { Directory.Delete(_downloadFolder, true); } } catch (Exception ex) { AppendToLog(ex.Message, true); } } _connectionId = 0; _mainFileUploaded = false; lblConnectionId.Text = "Id: "; lstBoxTasks.Items.Clear(); ProgressBarReset(progressBarUploads); ProgressBarReset(progressBarOperations); ProgressBarReset(progressBarDownloads); }