通过C#学Proto.Actor模型》之Remote
2024-10-13 02:34:15
Proto.Actor中提供了基于tcp/ip的通迅来实现Remote,可以通过其Remot实现对Actor的调用。
先来看一个极简单片的远程调用。
码友看码:
引用NuGet包
Proto.Actor
Proto.Remote
Proto.Serialization.Wire
共享库:
namespace P009_Lib
{
public class HelloRequest
{
public string Message
{
get; set;
}
}
public class HelloResponse
{
public string Message
{ get; set; }
}
}
服务端:
using P009_Lib;
using Proto;
using Proto.Remote;
using Proto.Serialization.Wire;
using System; using System.Threading;
using System.Threading.Tasks; namespace P009_Server
{
class Program
{
static void Main(string[] args)
{
Console.Title = "服务端";
Console.WriteLine("回车开始");
Console.ReadLine();
//设置序列化类型并注册
var wire = new WireSerializer(new[] { typeof(HelloRequest), typeof(HelloResponse) });
Serialization.RegisterSerializer(wire, true); var props = Actor.FromProducer(() => new HelloQuestActor());
//注册一个为hello类别的
Remote.RegisterKnownKind("hello", props);
//服务端监控端口5001
Remote.Start("127.0.0.1", );
Console.WriteLine("服务端开始……");
Console.ReadLine();
}
} class HelloQuestActor : IActor
{
public Task ReceiveAsync(IContext context)
{
switch (context.Message)
{
case HelloRequest msg:
Console.WriteLine(msg.Message);
context.Respond(new HelloResponse
{
Message = $"回应:我是服务端【{DateTime.Now}】",
});
break;
}
return Actor.Done;
}
}
}
客户端:
using P009_Lib;
using Proto;
using Proto.Remote;
using Proto.Serialization.Wire;
using System;
using System.Threading.Tasks; namespace P009_Client
{
class Program
{
static void Main(string[] args)
{ Console.Title = "客户端";
Console.WriteLine("回车开始");
Console.ReadLine();
//设置序列化类型并注册
var wire = new WireSerializer(new[] { typeof(HelloRequest), typeof(HelloResponse) });
Serialization.RegisterSerializer(wire, true);
//设置自己监控端口5002
Remote.Start("127.0.0.1", );
//连接服务端5001
var pid = Remote.SpawnNamedAsync("127.0.0.1:5001", "clientActor", "hello", TimeSpan.FromSeconds()).Result.Pid;
while (true)
{
var res = pid.RequestAsync<HelloResponse>(new HelloRequest { Message = $"请求:我是客户端 【{DateTime.Now}】" }).Result;
Console.WriteLine(res.Message);
Console.ReadLine();
}
}
}
}
代码很简单,看注释就够了。
……
最新文章
- jquery-三级联动
- PHP中使用CURL请求页面,使用fiddler进行抓包
- View基础知识
- Redis使用及优化入门
- 【9-15】python学习笔记01
- centos 6+安装山逗斯骚尅特(本文内容来自都比更具帝)
- DIV布局之道二:DIV块的嵌套,DIV盒子模型
- IT痴汉的工作现状18-思维定式
- exit() _exit()
- 关于odbc连接orcal,用户名密码大小写敏感问题
- xp添加右键";打开文件所在位置";
- Linux常用Shell脚本珍藏【转载】
- Linux select I/O 复用
- Angular企业级开发(9)-前后端分离之后添加验证码
- Python的join()函数和split()函数
- mysqldump备份还原mysql
- mysql之 binlog维护详细解析(开启、binlog相关参数作用、mysqlbinlog解读、binlog删除)
- 大数的减法函数--c语言
- 垃圾陷阱洛谷dp
- 特征提取方法: one-hot 和 TF-IDF
热门文章
- CAN总线学习记录之一:CAN简介
- SSH连接GitHub并配置ssh key
- vue axios 批量删除 数组参数
- file_get_contents(&#39;php://input&#39;) 和POST的区别
- C# 如何隐藏或显示工作表中的网格线
- Java消息中间件----ActiveMQ入门①
- 预计2019年发布的Vue3.0到底有什么不一样的地方?
- 从.Net到Java学习第五篇——Spring Boot &;&;Profile &;&;Swagger2
- Apex 中操作用户和组
- word中字体大小(pt)和网页中css设置font-size时用的px大小对应关系