/// <summary> /// Constructor /// </summary> /// <param name="manager">Manager</param> /// <param name="functionName">Function name</param> /// <param name="callback">Callback for data</param> /// <param name="param">End point parameters, each array of strings is a separate call to the end point function</param> /// <returns>Connection</returns> public async Task OpenAsync(SignalrManager manager, string functionName, Action <string> callback, string[][] param = null) { if (callback != null) { param = (param ?? new string[][] { new string[0] }); manager.AddListener(functionName, callback, param); string functionFullName = manager.GetFunctionFullName(functionName); bool result = true; foreach (string[] p in param) { result &= await manager.hubProxy.Invoke <bool>(functionFullName, p); } if (result) { this.manager = manager; this.callback = callback; this.functionFullName = functionFullName; lock (manager.sockets) { manager.sockets.Add(this); } return; } // fail, remove listener manager.RemoveListener(functionName, callback); } throw new APIException("Unable to open web socket to Bittrex"); }
/// <summary> /// Constructor /// </summary> /// <param name="functionName">Function name</param> /// <param name="callback">Callback for data</param> /// <param name="delayMilliseconds">Delay after invoking each object[] in param, used if the server will disconnect you for too many invoke too fast</param> /// <param name="param">End point parameters, each array of strings is a separate call to the end point function. For no parameters, pass null.</param> /// <returns>Connection</returns> public async Task OpenAsync(string functionName, Action <string> callback, int delayMilliseconds = 0, object[][] param = null) { if (callback != null) { param = (param ?? new object[][] { new object[0] }); manager.AddListener(functionName, callback, param); string functionFullName = manager.GetFunctionFullName(functionName); Exception ex = null; try { for (int i = 0; i < param.Length; i++) { if (i != 0) { await Task.Delay(delayMilliseconds); } if (!(await manager.hubProxy.Invoke <bool>(functionFullName, param[i]))) { throw new APIException("Invoke returned success code of false"); } } } catch (Exception _ex) { ex = _ex; Console.WriteLine("Error invoking hub proxy {0}: {1}", functionFullName, ex); } if (ex == null) { this.callback = callback; this.functionFullName = functionFullName; lock (manager.sockets) { manager.sockets.Add(this); } return; } // fail, remove listener manager.RemoveListener(functionName, callback); throw ex; } throw new ArgumentNullException(nameof(callback)); }
/// <summary> /// Constructor /// </summary> /// <param name="functionName">Function name</param> /// <param name="callback">Callback for data</param> /// <param name="delayMilliseconds">Delay after invoking each object[] in param, used if the server will disconnect you for too many invoke too fast</param> /// <param name="param">End point parameters, each array of strings is a separate call to the end point function. For no parameters, pass null.</param> /// <returns>Connection</returns> public async Task OpenAsync(string functionName, Func <string, Task> callback, int delayMilliseconds = 0, object[][] param = null) { if (callback != null) { SignalrManager _manager = this.manager; if (_manager == null) { throw new ArgumentNullException("SignalrManager is null"); } param = (param ?? new object[][] { new object[0] }); await _manager.AddListener(functionName, callback, param); // ask for proxy after adding the listener, as the listener will force a connection if needed IHubProxy _proxy = _manager.hubProxy; if (_proxy == null) { throw new ArgumentNullException("Hub proxy is null"); } string functionFullName = _manager.GetFunctionFullName(functionName); Exception ex = null; try { for (int i = 0; i < param.Length; i++) { if (i != 0) { await Task.Delay(delayMilliseconds); } if (!(await _proxy.Invoke <bool>(functionFullName, param[i]))) { throw new APIException("Invoke returned success code of false"); } } } catch (Exception _ex) { ex = _ex; Console.WriteLine("Error invoking hub proxy {0}: {1}", functionFullName, ex); } if (ex == null) { this.callback = callback; this.functionFullName = functionFullName; lock (_manager.sockets) { _manager.sockets.Add(this); } return; } // fail, remove listener _manager.RemoveListener(functionName, callback); throw ex; } throw new ArgumentNullException(nameof(callback)); }
/// <summary> /// Constructor /// </summary> /// <param name="manager">Manager</param> /// <param name="functionName">Function name</param> /// <param name="callback">Callback for data</param> /// <param name="param">End point parameters</param> /// <returns>Connection</returns> public async Task OpenAsync(SignalrManager manager, string functionName, Action <string> callback, params object[] param) { if (callback != null) { manager.AddListener(functionName, callback, param); string functionFullName = manager.GetFunctionFullName(functionName); bool result = await manager.hubProxy.Invoke <bool>(functionFullName, param); if (result) { this.manager = manager; this.callback = callback; this.functionFullName = functionFullName; return; } // fail, remove listener manager.RemoveListener(functionName, callback); } throw new APIException("Unable to open web socket to Bittrex"); }
/// <summary> /// Dispose of the socket and remove from listeners /// </summary> public void Dispose() { try { lock (manager.sockets) { manager.sockets.Remove(this); } manager.RemoveListener(functionFullName, callback); Disconnected?.Invoke(this); } catch { } }
/// <summary> /// Dispose of the socket and remove from listeners /// </summary> public void Dispose() { if (disposed) { return; } disposed = true; try { lock (manager.sockets) { manager.sockets.Remove(this); } manager.RemoveListener(functionFullName, callback); InvokeDisconnected().Sync(); } catch { } }
/// <summary> /// Constructor /// </summary> /// <param name="functionName">Function name</param> /// <param name="callback">Callback for data</param> /// <param name="delayMilliseconds">Delay after invoking each object[] in param, used if the server will disconnect you for too many invoke too fast</param> /// <param name="param">End point parameters, each array of strings is a separate call to the end point function. For no parameters, pass null.</param> /// <returns>Connection</returns> public async Task OpenAsync(string functionName, Func <string, Task> callback, int delayMilliseconds = 0, object[][] param = null) { if (callback == null) { throw new ArgumentNullException(nameof(callback)); } SignalrManager _manager = this.manager; if (_manager == null) { throw new ArgumentNullException("SignalrManager is null"); } Exception ex = null; param = (param ?? new object[][] { new object[0] }); string functionFullName = _manager.GetFunctionFullName(functionName); this.functionFullName = functionFullName; while (true) { await _manager.AddListener(functionName, callback, param); try { // ask for proxy after adding the listener, as the listener will force a connection if needed IHubProxy _proxy = _manager.hubProxy; if (_proxy == null) { throw new ArgumentNullException("Hub proxy is null"); } // all parameters must succeed or we will give up and try the loop all over again for (int i = 0; i < param.Length; i++) { if (i != 0) { await Task.Delay(delayMilliseconds); } bool result = await _proxy.Invoke <bool>(functionFullName, param[i]).ConfigureAwait(false); if (!result) { throw new APIException("Invoke returned success code of false"); } } break; } catch (Exception _ex) { // fail, remove listener _manager.RemoveListener(functionName, callback); ex = _ex; Logger.Info("Error invoking hub proxy {0}: {1}", functionFullName, ex); if (disposed || manager.disposed) { // give up, if we or the manager is disposed we are done break; } else { // try again in a bit... await Task.Delay(500); } } } if (ex == null) { this.callback = callback; lock (_manager.sockets) { _manager.sockets.Add(this); } return; } }
/// <summary> /// Constructor /// </summary> /// <param name="functionName">Function name</param> /// <param name="callback">Callback for data</param> /// <param name="delayMilliseconds">Delay after invoking each object[] in param, used if the server will disconnect you for too many invoke too fast</param> /// <param name="param">End point parameters, each array of strings is a separate call to the end point function. For no parameters, pass null.</param> /// <returns>Connection</returns> public async Task OpenAsync(string functionName, Func <string, Task> callback, int delayMilliseconds = 0, object[][] param = null) { callback.ThrowIfNull(nameof(callback), "Callback must not be null"); SignalrManager _manager = this.manager; _manager.ThrowIfNull(nameof(manager), "Manager is null"); Exception ex = null; param = (param ?? new object[][] { new object[0] }); string functionFullName = _manager.GetFunctionFullName(functionName); this.functionFullName = functionFullName; while (!_manager.disposed) { await _manager.AddListener(functionName, callback, param); if (_manager.hubConnection.State != ConnectionState.Connected) { await Task.Delay(100); continue; } try { // ask for proxy after adding the listener, as the listener will force a connection if needed IHubProxy _proxy = _manager.hubProxy; if (_proxy == null) { throw new ArgumentNullException("Hub proxy is null"); } // all parameters must succeed or we will give up and try the loop all over again for (int i = 0; i < param.Length; i++) { if (i != 0) { await Task.Delay(delayMilliseconds); } bool result = await _proxy.Invoke <bool>(functionFullName, param[i]).ConfigureAwait(false); if (!result) { throw new APIException("Invoke returned success code of false"); } } break; } catch (Exception _ex) { // fail, remove listener _manager.RemoveListener(functionName, callback); ex = _ex; Logger.Info("Error invoking hub proxy {0}: {1}", functionFullName, ex); if (disposed || manager.disposed) { // give up, if we or the manager is disposed we are done break; } else { // try again in a bit... await Task.Delay(500); } } } if (ex == null) { this.callback = callback; lock (_manager.sockets) { _manager.sockets.Add(this); } if (!initialConnectFired) { initialConnectFired = true; // kick off a connect event if this is the first time, the connect even can only get set after the open request is sent Task.Delay(1000).ContinueWith(async(t) => { await InvokeConnected(); }).ConfigureAwait(false).GetAwaiter(); } return; } }