using Longbow.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
namespace Bootstrap.Admin
{
///
///
///
internal class DefaultOnlineUsers : IOnlineUsers
{
private ConcurrentDictionary _onlineUsers = new ConcurrentDictionary();
private HttpClient _client;
private IEnumerable _local = new string[] { "::1", "127.0.0.1" };
///
///
///
///
public DefaultOnlineUsers(IHttpClientFactory factory)
{
_client = factory.CreateClient(OnlineUsersServicesCollectionExtensions.IPSvrHttpClientName);
}
///
///
///
///
public IEnumerable OnlineUsers
{
get { return _onlineUsers.Values.Select(v => v.User); }
}
///
///
///
///
///
///
///
public OnlineUserCache AddOrUpdate(string key, Func addValueFactory, Func updateValueFactory) => _onlineUsers.AddOrUpdate(key, addValueFactory, updateValueFactory);
///
///
///
///
///
///
public bool TryRemove(string key, out OnlineUserCache onlineUserCache) => _onlineUsers.TryRemove(key, out onlineUserCache);
///
///
///
///
///
public string RetrieveLocaleByIp(string ip = null)
{
if (ip.IsNullOrEmpty() || _local.Any(p => p == ip)) return "本地连接";
var url = ConfigurationManager.AppSettings["IPSvrUrl"];
var task = _client.GetAsJsonAsync($"{url}{ip}");
task.Wait();
return task.Result.status == "0" ? string.Join(" ", task.Result.address.SpanSplit("|").Skip(1).Take(2)) : "XX XX";
}
///
///
///
private class IPLocator
{
///
/// 详细地址信息
///
public string address { get; set; }
///
/// 结果状态返回码
///
public string status { get; set; }
}
}
}