gpt4 book ai didi

c# - 我如何在 namedPipeWrapper 中在服务器和客户端之间推送消息?

转载 作者:行者123 更新时间:2023-12-02 18:50:24 25 4
gpt4 key购买 nike

我根据文档写了客户端和服务器端Named Pipe Wrapper ,但不幸的是只有 server.ClientConnected 运行而 server.ClientMessage 和 client.ServerMessage 不运行。如何将消息从客户端推送到服务器,反之亦然?

型号:

[Serializable]
public class SomeClass
{
public string Request { get; set; }
public string Response { get; set; }
}

客户端:

var client = new NamedPipeClient<SomeClass>("MyServerPipe");

client.ServerMessage += delegate (NamedPipeConnection<SomeClass, SomeClass> conn, SomeClass message)
{
Console.WriteLine("Server says: {0}", message.Response);
};

// Start up the client asynchronously and connect to the specified server pipe.
// This method will return immediately while the client runs in a separate background thread.
client.Start();
Console.ReadKey();

服务器端:

 var server = new NamedPipeServer<SomeClass>("MyServerPipe");

server.ClientConnected += delegate (NamedPipeConnection<SomeClass, SomeClass> conn)
{
Console.WriteLine("Client {0} is now connected!", conn.Id);
conn.PushMessage(new SomeClass { Response= "Welcome!" });
};

server.ClientMessage += delegate (NamedPipeConnection<SomeClass, SomeClass> conn, SomeClass message)
{
Console.WriteLine("Client {0} says: {1}", conn.Id, message.Request);
};

// Start up the server asynchronously and begin listening for connections.
// This method will return immediately while the server runs in a separate background thread.
server.Start();
Console.ReadKey();

最佳答案

基于 documentation

要测试管道的性能,最好将 ExampleCLI 项目转换为两个客户端和服务器项目,并将 SomeClass 模型移动到新的类库中。当模型可序列化时,您的问题就会出现。这就是为什么不调用 server.ClientMessageclient.ServerMessage 的原因。

关于c# - 我如何在 namedPipeWrapper 中在服务器和客户端之间推送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66877297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com