Esempio n. 1
0
        public async Task <bool> AppClientsDoActionAsync(string address, string appId, WebsocketAction action)
        {
            var result = await FunctionUtil.TRY(async() =>
            {
                using (var resp = await(address + "/RemoteOP/AppClientsDoAction".AppendQueryString("appId", appId))
                                  .AsHttp("POST", action)
                                  .Config(new RequestOptions {
                    ContentType = "application/json"
                })
                                  .SendAsync())
                {
                    if (resp.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        var result = resp.Deserialize <dynamic>();

                        if ((bool)result.success)
                        {
                            return(true);
                        }
                    }

                    return(false);
                }
            }, 5);

            await _sysLogService.AddSysLogAsync(new SysLog
            {
                LogTime = DateTime.Now,
                LogType = result ? SysLogType.Normal : SysLogType.Warn,
                AppId   = appId,
                LogText = $"通知节点{address}应用{appId}的客户端:{action.Action} 响应:{(result ? "成功" : "失败")}"
            });

            return(result);
        }
Esempio n. 2
0
        public async Task <bool> OneClientDoActionAsync(string address, string clientId, WebsocketAction action)
        {
            var result = await FunctionUtil.TRY(async() =>
            {
                using (var resp = await(address + "/RemoteOP/OneClientDoAction?clientId=" + clientId)
                                  .AsHttp("POST", action)
                                  .Config(new RequestOptions {
                    ContentType = "application/json"
                })
                                  .SendAsync())
                {
                    if (resp.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        var result = resp.Deserialize <dynamic>();

                        if ((bool)result.success)
                        {
                            if (action.Action == ActionConst.Offline || action.Action == ActionConst.Remove)
                            {
                                if (_serverNodeClientReports.ContainsKey(address))
                                {
                                    if (_serverNodeClientReports[address].Infos != null)
                                    {
                                        var report = _serverNodeClientReports[address];
                                        report.Infos.RemoveAll(c => c.Id == clientId);
                                        report.ClientCount = report.Infos.Count;
                                    }
                                }
                            }

                            return(true);
                        }
                    }

                    return(false);
                }
            }, 5);

            using (var service = GetSysLogService())
            {
                await service.AddSysLogAsync(new SysLog
                {
                    LogTime = DateTime.Now,
                    LogType = result ? SysLogType.Normal : SysLogType.Warn,
                    LogText = $"通知节点【{address}】的客户端【{clientId}】:【{action.Action}】 响应:{(result ? "成功" : "失败")}"
                });
            }

            return(result);
        }
        public Task TestEchoAsync()
        {
            return(Task.Run(async() =>
            {
                while (true)
                {
                    var nodes = await _serverNodeService.GetAllNodesAsync();
                    nodes.ForEach(n =>
                    {
                        try
                        {
                            FunctionUtil.TRY(() =>
                            {
                                using (var resp = (n.Address + "/home/echo").AsHttp().Send())
                                {
                                    if (resp.StatusCode == System.Net.HttpStatusCode.OK && resp.GetResponseContent() == "ok")
                                    {
                                        n.LastEchoTime = DateTime.Now;
                                        n.Status = Data.Entity.NodeStatus.Online;
                                        var report = GetClientReport(n);
                                        if (_serverNodeClientReports.ContainsKey(n.Address))
                                        {
                                            _serverNodeClientReports[n.Address] = report;
                                        }
                                        else
                                        {
                                            _serverNodeClientReports.AddOrUpdate(n.Address, report, (k, r) => report);
                                        }
                                    }
                                    else
                                    {
                                        n.Status = Data.Entity.NodeStatus.Offline;
                                    }
                                    _serverNodeService.UpdateAsync(n);
                                }
                            }, 5);
                        }
                        catch (Exception e)
                        {
                            _logger.LogInformation(e, "Try test node {0} echo , but fail .", n.Address);
                        }
                    });

                    await Task.Delay(5000 * 1);
                }
            }));
        }
        public async Task TestEchoAsync(string address)
        {
            var node = await _serverNodeService.GetAsync(address);

            try
            {
                FunctionUtil.TRY(() =>
                {
                    using (var resp = (node.Address + "/home/echo").AsHttp().Send())
                    {
                        if (resp.StatusCode == System.Net.HttpStatusCode.OK && resp.GetResponseContent() == "ok")
                        {
                            node.LastEchoTime = DateTime.Now;
                            node.Status       = Data.Entity.NodeStatus.Online;
                            var report        = GetClientReport(node);
                            if (report != null)
                            {
                                if (_serverNodeClientReports.ContainsKey(node.Address))
                                {
                                    _serverNodeClientReports[node.Address] = report;
                                }
                                else
                                {
                                    _serverNodeClientReports.AddOrUpdate(node.Address, report, (k, r) => report);
                                }
                            }
                        }
                        else
                        {
                            node.Status = Data.Entity.NodeStatus.Offline;
                        }
                        _serverNodeService.UpdateAsync(node);
                    }
                }, 5);
            }
            catch (Exception e)
            {
                _logger.LogInformation(e, "Try test node {0} echo , but fail .", node.Address);
            }
        }
        private ClientInfos GetClientReport(ServerNode node)
        {
            return(FunctionUtil.TRY(() =>
            {
                using (var resp = (node.Address + "/report/Clients").AsHttp().Config(new RequestOptions(new SerializeProvider())).Send())
                {
                    if (resp.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        var content = resp.GetResponseContent();
                        _logger.LogTrace($"ServerNode: {node.Address} report clients infomation , {content}");

                        var report = resp.Deserialize <ClientInfos>();
                        if (report != null)
                        {
                            return report;
                        }
                    }

                    return null;
                }
            }, 5));
        }