【问题标题】:Unable to send messages between Unity3d and Python using NetMQ无法使用 NetMQ 在 Unity3d 和 Python 之间发送消息
【发布时间】:2021-07-14 05:01:32
【问题描述】:

我想使用 ZMQ 在 Android 和 Python 之间来回传输消息。我正在使用 NetMQ/ZMQ 这样做。以下是我的请求者代码。

public class Requester : RunAbleThread
{
    
    protected override void Run()
    {
        ForceDotNet.Force(); 
        using (RequestSocket client = new RequestSocket())
        {
            client.Connect("tcp://localhost:5556");

            for (int i = 0; i < 10 && Running; i++)
            {
                Debug.Log("Sending Hello");
                client.SendFrame("Hello");

                string message = null;
                bool gotMessage = false;
                while (Running)
                {
                    gotMessage = client.TryReceiveFrameString(out message); 
                    if (gotMessage) break;
                }

                if (gotMessage) Debug.Log("Received " + message);
            }
        }

        NetMQConfig.Cleanup(); 
    }
}

以下是我的接收者的代码。

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

while True:
    #  Wait for next request from client
    message = socket.recv()
    print("Received request: %s" % message)

    socket.send(b"World")

以上代码在编辑器模式下运行良好。但是,当我在 Android 中运行相同的代码时,请求者根本没有收到任何消息。我错过了什么?两台设备都连接到同一个WIFI热点。

【问题讨论】:

    标签: python c# android unity3d zeromq


    【解决方案1】:

    你正在使用

    client.Connect("tcp://localhost:5556")
    

    localhost 就是您的设备本身!

    它在编辑器中工作,因为编辑器和 python 都在同一设备上运行!

    由于您的手机上没有运行python服务器,因此尝试连接到手机的端口5556是没有用的;)

    您宁愿使用与手机在同一网络中的 PC 的 IP。比如说

    client.Connect("tcp://192.168.1.234:5556")
    

    【讨论】:

      猜你喜欢
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多