gpt4 book ai didi

c# - 如何正确使用 NetMq Poller 接收数据

转载 作者:行者123 更新时间:2023-11-30 12:54:45 25 4
gpt4 key购买 nike

我的任务是创建一个接口(interface),通过套接字从发送方接收数据,为此我在发送方使用 NetMQ PushSocket,然后我在客户端 sung PullSocket 接收数据,我必须收到数据时更新 UI(WPF 应用程序),因此当我在单独的服务类中执行此操作并在 UI ViewModel 中调用该类时,我使用 PullSocket 的 ReceiveReady 事件中的轮询器接收数据,UI 线程挂起,因此我使用 Poller.Run在一个任务中,现在的问题是当我停止轮询器然后再次重新启动它时,它不会调用 ReceiveReady 事件

这里是接收数据的ReceiverService。

public class ReceiverService
{
string msg;
string _address;
int _port;
PullSocket receiver;
NetMQPoller poller;
private MapViewModel ViewModel { get; set; }
public ReceiverService(MapViewModel mapViewModel, int port = 5555)
{
_address = GetComputerLanIP();
_port = port;
receiver = new PullSocket($"tcp://{_address}:{_port}");
receiver.Options.Linger = TimeSpan.Zero;
this.ViewModel = mapViewModel;
poller = new NetMQPoller { receiver };
receiver.ReceiveReady += receiver_ReceiveReady;
}
public void Start()
{
receiver.Connect($"tcp://{_address}:{_port}");
poller.Run();
}
public void Stop()
{
receiver.Disconnect($"tcp://{_address}:{_port}");
poller.Stop();
}

private void receiver_ReceiveReady(object sender, NetMQSocketEventArgs e)
{
// receive won't block as a message is ready
msg = e.Socket.ReceiveFrameString();
// send a response
if (!string.IsNullOrEmpty(msg))
{
try
{
//Updaing the ViewModel here
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
private string GetComputerLanIP()
{
string strHostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);

foreach (var ipAddress in ipEntry.AddressList)
{
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
{
return ipAddress.ToString();
}
}
return "";
}
private string GetValueFromMessage(string identifier)
{
msg.Replace("{", "");
msg.Replace("}", "");
identifier = /*" " + */identifier + " = ";
try
{
int index = msg.IndexOf(identifier) + identifier.Length;

if (index != -1)
{
int index2 = msg.IndexOf(";", index);
if (index2 == -1)
{
index2 = msg.Length;
}
return msg.Substring(index, index2 - index);
}
}
catch (IndexOutOfRangeException ex)
{
return null;
}
return null;
}
}

在我的 ViewModel 中,我已经为这些设置了命令

private void StartReceiver()
{
Task.Run(() => ReceiverService.Start());
}
private void StopReceiver()
{
Task.Run(() => ReceiverService.Stop());
}

我做错了什么?我是 NetMQ 和 WPF 的新手。时间差

最佳答案

  1. 起初最好在 ReceiverService 中创建一个任务,类似于 ActorModel,因为最后如果您想在任何地方重用它,您需要记住您应该先创建一个任务。
  2. 在 using 语句中总是有 socket 会很好,因为如果你不使用它,你应该总是关闭 socket
 public async Task StartAsync()        {
await Task.Run(() => ThreadBody())
}
public void Stop()
{
_poller.Stop();
}
private void ThreadBody()
{
using (PullSocket receiverSocket = new PullSocket())
using (_poller = new NetMQPoller())
{
receiverSocket.Connect($"tcp://{_address}:{_port}");
receiverSocket.ReceiveReady += receiver_ReceiveReady;
_poller.Add(receiverSocket);
_poller.Run();
}
}

关于c# - 如何正确使用 NetMq Poller 接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53541567/

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