Xamarin使XRPC实现接口/委托远程调用
生活随笔
收集整理的這篇文章主要介紹了
Xamarin使XRPC实现接口/委托远程调用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
????????在之前的文章中已經介紹如何使用Beetlex.XRCP組件進行接口/委托遠程調用;由于組件BeetleX.XRPC.Clients支持.NETStandard2,因此Xamarin同樣可以使用它來實現基于接口/委托的數據交互通訊。
????????接下來通過Xamarin實現一個簡單的移動程序,并通過XRPC的功能基于遠程接口調用的方式登陸到服務端,然后再通過委托遠程調用的方式來相互獲取對方的系統和時間信息。
服務端定義
????????服務主要定義一個用戶接口并實現登陸方法,然后注冊一個委托用于返回服務信息和時間給客戶端。
以上服務使用了TLS作為基礎安全通訊。啟動服務后會顯示以下日志
客戶端定義
??????? Xamarin下使用XRPC只需要引用BeetleX.XRPC.Clients即可,引用完成后可以定義XRPCClient對象來進行通訊。
public partial class MainPage : ContentPage {public MainPage(){InitializeComponent();//定義XRPC客戶端mClient = new BeetleX.XRPC.Clients.XRPCClient("192.168.1.18", 9090, "beetlex");mClient.CertificateValidationCallback = (s, certificate, chain, sslPolicyErrors) => true;mClient.Options.ParameterFormater = new JsonPacket();//創建接口遠程接口代理mUser = mClient.Create<IUser>();//定義委托給服務端調用mClient.AddDelegate<Func<Task<string>>>(() =>{return Task.FromResult($"{Environment.OSVersion} {DateTime.Now}");});//創建對應服務端的遠程委托代理mGetTime = mClient.Delegate<Func<Task<string>>>();}private Func<Task<string>> mGetTime;private static IUser mUser;private static BeetleX.XRPC.Clients.XRPCClient mClient;private async void Button_Clicked(object sender, EventArgs e){try{if (string.IsNullOrEmpty(userName.Text)){await DisplayAlert("Error", "Enter you name!", "OK");return;}//登陸var result = await mUser.Login(userName.Text, userPwd.Text);this.layoutGrid.IsVisible = false;this.cmdLogin.IsVisible = false;OnGetTime();}catch (Exception e_){await DisplayAlert("Error", e_.Message, "OK");}}private async Task OnGetTime(){try{//定時獲取服務和時間信息while (true){var result = await mGetTime();txtResult.Text = result;await Task.Delay(1000);}}catch (Exception e_){await DisplayAlert("Error", e_.Message, "OK");this.layoutGrid.IsVisible = true;this.cmdLogin.IsVisible = true;}} } BeetleX開源跨平臺通訊框架(支持TLS)
輕松實現高性能:tcp、http、websocket、redis、rpc和網關等服務應用
https://beetlex.io
如果你想了解某方面的知識或文章可以把想法發送到
henryfan@msn.com|admin@beetlex.io
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Xamarin使XRPC实现接口/委托远程调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IdentityServer4 之 Re
- 下一篇: 浅谈CLR基础知识