简体   繁体   English

ZeroMQ 在 Unity 中不起作用,但可以作为控制台应用程序使用

[英]ZeroMQ doesn't work in Unity, but works as console application

I try to connect my python process with Unity game through ZeroMQ.我尝试通过 ZeroMQ 将我的 python 进程与 Unity 游戏连接起来。 I have a python script server.py which sends a message through pair socket我有一个 python 脚本server.py通过对套接字发送消息

import zmq

context = zmq.Context()
socket = context.socket(zmq.PAIR)
socket.bind(f"tcp://*:5556")

socket.send(b"Server message to client")
print("Sent")

And I have a MonoBehaviour script attached to a GameObject within Unity, which looks like this:我有一个MonoBehaviour脚本附加到 Unity 中的游戏对象,如下所示:

public class AiManager : MonoBehaviour
{
    void Start()
    {
        using (var server = new PairSocket())
        {
            server.Connect("tcp://localhost:5556");
            var msg = server.ReceiveFrameString();
            Debug.Log(msg);
        }
    }
}

The game hangs on ReceiveFrameString() forever and crashes Unity.游戏永远挂在ReceiveFrameString()上并导致 Unity 崩溃。 Also server.py hangs and doesn't send the message. server.py也挂起并且不发送消息。

However I created an independent console application, with the exact same code:但是我创建了一个独立的控制台应用程序,代码完全相同:

class Program
{
    static void Main(string[] args)
    {
        using (var server = new PairSocket())
        {
            server.Connect("tcp://localhost:5556");
            var msg = server.ReceiveFrameString();
            Console.WriteLine($"From Server: {msg}");
        }
    }
}

This application works perfectly.此应用程序完美运行。

How can I fix that?我该如何解决?

It can be a total PITA ensuring that Unity (ie, the Unity app) simply has permission to talk locally.它可以是一个完整的 PITA,确保 Unity(即 Unity 应用程序)仅具有在本地通话的权限。

I assume you're on PC, not Mac, examine carefully if Unity is allowed to make such network connections.我假设您使用的是 PC,而不是 Mac,请仔细检查 Unity 是否允许进行此类网络连接。

Windows firewall settings can be a total PITA for Unity. Windows 防火墙设置可以是Unity的总PITA。

Read various threads, https://forum.unity.com/threads/solved-multiplayer-networking-lan-wont-connect-from-another-pc.470459/阅读各种线程, https://forum.unity.com/threads/solved-multiplayer-networking-lan-wont-connect-from-another-pc.470459/

One solution is, spin up a real remote server on AWS, and try that.一种解决方案是,在 AWS 上启动一个真正的远程服务器,然后尝试一下。 It's often easier than getting localhost to work.这通常比让 localhost 工作更容易。

One shitty problem is that quite simply, on some versions of Windows the stupid "OK" dialog which appears actually appears behind Unity, doh, can waste hours.一个糟糕的问题很简单,在 Windows 的某些版本上,愚蠢的“确定”对话框实际上出现在 Unity后面,doh,可能会浪费数小时。

Alright, after multiple hours of experimenting and browsing the internet I have fixed the problem.好吧,经过几个小时的试验和浏览互联网后,我已经解决了这个问题。

First thing to do, thanks to @jdweng's comment is to first send a message from client to server.感谢@jdweng 的评论,首先要做的事情是首先从客户端向服务器发送消息。

Server doesn't do anything when a connection is made.建立连接时,服务器不做任何事情。 The server is a slave and waits for a client to send a command.服务器是从机,等待客户端发送命令。 [...] It behaves differently as console application, because you are dealing with a different kernel/operating system with different drivers. [...] 它与控制台应用程序的行为不同,因为您正在处理具有不同驱动程序的不同内核/操作系统。

Adding a server.SendFrameEmpty();添加一个server.SendFrameEmpty(); to the client and socket.recv() to the server fixes the problem, but Unity still freezes.到客户端和socket.recv()到服务器解决了这个问题,但 Unity 仍然冻结。

I found this answer saying:我发现这个答案说:

The key is to have AsyncIO.ForceDotNet.Force();关键是要有AsyncIO.ForceDotNet.Force(); before you start and NetMQConfig.Cleanup();在你开始之前和NetMQConfig.Cleanup(); before you end.在你结束之前。

Finally the whole working code looks like this最后整个工作代码看起来像这样

public class AiManager : MonoBehaviour
{
    void Start()
    {
        AsyncIO.ForceDotNet.Force();
        using (var server = new PairSocket())
        {
            server.Connect("tcp://localhost:5556");
            server.SendFrameEmpty();
            var msg = server.ReceiveFrameString();
            Debug.Log(msg);
        }
        NetMQConfig.Cleanup();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么zeromq不能在localhost上运行? - Why doesn't zeromq work on localhost? pycharm和子进程 - 在控制台中工作的东西在Pycharm中不起作用 - pycharm and subprocess - what works in console doesn't work in Pycharm 具有open('filename')的Python脚本可用于IDLE,但不能在控制台中使用 - Python script with open('filename') works with IDLE but doesn't work in console 代码在 IDE 中有效,但在终端控制台中无效 - Code works in IDE but doesn't work in Terminal Console JavaScript 适用于 chrome 控制台,但不适用于 firefox 或 selenium 代码 - JavaScript works on chrome console but doesn't work in firefox or selenium code 为什么 ZeroMQ 演示代码在 Win10 上不起作用? - Why a ZeroMQ demo code doesn't work on Win10? 使用 function 无法为 tkinter window 生成菜单,但如果在 python 控制台上执行这些步骤,则工作正常 - Generating a menu for a tkinter window doesn’t work using a function but works just fine if the steps are executed on a python console UDP 协议在 ZeroMQ 中不起作用。 应该使用哪种插座类型? - UDP-protocol doesn't work in ZeroMQ. Which socket type should be used? 将控制台输出导出到.txt不起作用 - Exporting console output to a .txt doesn't work ipdb在vim控制台中不起作用 - ipdb doesn't work in vim console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM