/// <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 (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="manager">Manager</param> public SignalrSocketConnection(SignalrManager manager) { this.manager = manager; }