Skip to content

Commit

Permalink
使用客户端基类来简化IoT的Http客户端
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed May 16, 2024
1 parent 31f0762 commit 6b47143
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 322 deletions.
2 changes: 1 addition & 1 deletion NewLife.Remoting/Clients/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public virtual LoginRequest BuildLoginRequest()
}

/// <summary>获取心跳信息</summary>
public PingRequest BuildPingRequest()
public virtual PingRequest BuildPingRequest()
{
var request = new PingRequest
{
Expand Down
34 changes: 23 additions & 11 deletions NewLife.Remoting/Clients/HttpClientBase.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using NewLife.Caching;
using System.Diagnostics.CodeAnalysis;
using NewLife.Caching;
using NewLife.Log;
using NewLife.Remoting.Models;
using NewLife.Threading;
using NewLife.Serialization;
using System.Net.Http;
using System.Diagnostics.CodeAnalysis;

#if NETCOREAPP
using System.Net.WebSockets;
Expand Down Expand Up @@ -50,6 +49,11 @@ public HttpClientBase(String urls) : this()
}
}
}

/// <summary>新增服务点</summary>
/// <param name="name"></param>
/// <param name="url"></param>
public void AddService(String name, String url) => _client.Add(name, new Uri(url));
#endregion

#region 方法
Expand Down Expand Up @@ -232,14 +236,7 @@ private async Task DoPull(WebSocket socket, CancellationToken cancellationToken)
{
var data = await socket.ReceiveAsync(new ArraySegment<Byte>(buf), cancellationToken);
var txt = buf.ToStr(null, 0, data.Count);
if (txt.StartsWithIgnoreCase("Pong"))
{
}
else
{
var model = txt.ToJsonEntity<CommandModel>();
if (model != null) await ReceiveCommand(model);
}
await OnReceive(txt);
}
}
catch (Exception ex)
Expand All @@ -251,6 +248,21 @@ private async Task DoPull(WebSocket socket, CancellationToken cancellationToken)
}
#endif

/// <summary>收到服务端主动下发消息。默认转为CommandModel命令处理</summary>
/// <param name="message"></param>
/// <returns></returns>
protected virtual async Task OnReceive(String message)
{
if (message.StartsWithIgnoreCase("Pong"))
{
}
else
{
var model = message.ToJsonEntity<CommandModel>();
if (model != null) await ReceiveCommand(model);
}
}

async Task ReceiveCommand(CommandModel model)
{
if (model == null) return;
Expand Down
4 changes: 2 additions & 2 deletions NewLife.Remoting/IApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public class ApiHandler : IApiHandler
if (action.IsNullOrEmpty()) action = "Api/Info";

var manager = (Host as ApiServer)?.Manager;
var api = manager?.Find(action) ?? throw new ApiException(404, $"无法找到名为[{action}]的服务!");
var api = manager?.Find(action) ?? throw new ApiException(ApiCode.NotFound, $"无法找到名为[{action}]的服务!");

// 全局共用控制器,或者每次创建对象实例
var controller = manager.CreateController(api) ?? throw new ApiException(403, $"无法创建名为[{api.Name}]的服务!");
var controller = manager.CreateController(api) ?? throw new ApiException(ApiCode.Forbidden, $"无法创建名为[{api.Name}]的服务!");
if (controller is IApi capi) capi.Session = session;
if (session is INetSession ss)
api.LastSession = ss.Remote + "";
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Remoting/NewLife.Remoting.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;net461;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net45;net461;netstandard2.0;netstandard2.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<AssemblyTitle>RPC远程过程调用</AssemblyTitle>
<Description>RPC远程过程调用,二进制封装,提供高吞吐低延迟的高性能RPC框架</Description>
<Company>新生命开发团队</Company>
Expand Down
3 changes: 2 additions & 1 deletion NewLife.Remoting/Services/ICommandClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public static async Task<CommandReplyModel> ExecuteCommand(this ICommandClient c
{
//WriteLog("OnCommand {0}", model.ToJson());

if (!client.Commands.TryGetValue(model.Command, out var d)) throw new ApiException(400, $"找不到服务[{model.Command}]");
if (!client.Commands.TryGetValue(model.Command, out var d))
throw new ApiException(ApiCode.NotFound, $"找不到服务[{model.Command}]");

if (d is Func<String?, Task<String?>> func1) return await func1(model.Argument);
//if (d is Func<String, Task<Object>> func2) return await func2(model.Argument);
Expand Down
2 changes: 1 addition & 1 deletion Samples/IoTZero/Clients/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static async Task Main(IServiceProvider serviceProvider)
Log = XTrace.Log,
};

await device.LoginAsync();
await device.Login();

_device = device;
}
Expand Down
Loading

0 comments on commit 6b47143

Please sign in to comment.