PipeHelper 是对Pipe的简单封装,用于进程之间相互通信,开启两个管道,分别用来收和发,实现了自动重连,消息回调
$ dotnet add package ITLDG.PipeHelperPipeHelper 是对Pipe的简单封装,用于进程之间相互通信,开启两个管道,分别用来收和发,实现了自动重连,消息回调
程序在收到消息时会判断是否为PipeMsg格式,如果是则调用ReceiveMsg回调,否则调用ReceiveMessage回调
//创建管道对象(服务端)
ITLDG.PipeHelper.PipeServer pipe = new ITLDG.PipeHelper.PipeServer();
//创建管道对象(客户端)
//ITLDG.PipeHelper.PipeClient pipe = new ITLDG.PipeHelper.PipeClient();
//连接状态回调
pipe.ConnectedChanged += (bool connected, string pipeName) =>
{
Debug.WriteLine("连接状态:" + connected);
};
//收到对象消息回调
pipe.ReceiveMsg += (ITLDG.PipeHelper.PipeMsg msg, string pipeName) =>
{
//原文回复,如果无需回复返回null
return msg;
};
//收到文本消息回调
pipe.ReceiveMessage += (string msg, string pipeName) =>
{
//原文回复,如果无需回复返回null
return msg;
};
//开启/连接管道
//pipe.Start("ITLDG");
Task.Run(() =>
{
pipe.Start("ITLDG");
});
//关闭管道
pipe.Stop();
//主动发送消息
pipe.SendMessage(new ITLDG.PipeHelper.PipeMsg()
{
Success = true,
Action = "test",
Msg = "测试成功",
Data = "附加数据"
});