Ejemplo n.º 1
0
            /// <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;
                }
            }
Ejemplo n.º 2
0
            /// <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 (!disposed && !_manager.disposed)
                {
                    try
                    {
                        // performs any needed reconnect
                        await _manager.AddListener(functionName, callback, param);

                        while (!disposed && !_manager.disposed && _manager.hubConnection.State != ConnectionState.Connected)
                        {
                            await Task.Delay(100);
                        }

                        // 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);
                            }
                            if (!(await _proxy.Invoke <bool>(functionFullName, param[i])))
                            {
                                throw new APIException("Invoke returned success code of false");
                            }
                        }
                        ex = null;
                        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 && !disposed && !_manager.disposed)
                {
                    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 event can only get set after the open request is sent
                        Task.Run(async() =>
                        {
                            await Task.Delay(1000); // give time for the caller to set a connected event
                            await InvokeConnected();
                        }).ConfigureAwait(false).GetAwaiter();
                    }
                }
            }