无法使用 NetMQ 在 Unity3d 和 Python 之间发送消息
Unable to send messages between Unity3d and Python using NetMQ
我想使用 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 热点。
您正在使用
client.Connect("tcp://localhost:5556")
localhost 是您的设备本身!
它在编辑器中工作,因为编辑器和 python 在 同一设备上都是 运行!
由于 python 服务器不在 phone 上 运行,因此尝试连接到 phone 的端口 5556
是没有用的;)
您宁愿使用与 phone 在同一网络中的 PC 的 IP。比如
client.Connect("tcp://192.168.1.234:5556")
我想使用 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 热点。
您正在使用
client.Connect("tcp://localhost:5556")
localhost 是您的设备本身!
它在编辑器中工作,因为编辑器和 python 在 同一设备上都是 运行!
由于 python 服务器不在 phone 上 运行,因此尝试连接到 phone 的端口 5556
是没有用的;)
您宁愿使用与 phone 在同一网络中的 PC 的 IP。比如
client.Connect("tcp://192.168.1.234:5556")