使用zeroMQ,是否可以从我创建的Android应用发送(' Hello'消息)到在python中运行 .recv()
的命令行?
我用一个按钮创建了一个Android应用程序,一旦按下它发送"你好"到tcp://machine2:5555
。
按钮侦听器的摘录:
settingsButton.setOnClickListener(new OnClickListener(){
public void onClick(final View v) {
Toast toast = Toast.makeText(getApplicationContext(), "Playing video", Toast.LENGTH_SHORT);
toast.show();
// insert SSH command for ffmpeg here
ZMQ.Context ctx = ZMQ.context(1);
Socket soc = ctx.socket(ZMQ.REQ);
soc.connect("tcp://192.168.0.16:5555");
//System.out.print("established");
String msg = "hi";
soc.send(msg.getBytes(),0);
}
});
终端中运行的python接收器将显示收到的消息。
import zmq
import time
import subprocess
port = "5555"
#first create a context for zeroMQ to begin communication
context = zmq.Context()
#pair communication
socket = context.socket(zmq.REP)
#bind ip address and port opened for communication
socket.bind("tcp://*:%s" % port)
print "tcp://192.168.0.16"
while True:
msg = socket.recv()
if msg is not None:
print msg
#run_command(msg)[0]
#subprocess.call(["mplayer","/home/kevo/capture003.dv"])
subprocess.call(["time", "mplayer","/home/kevo/Desktop/small.mp4"])
time.sleep(1)
但到目前为止我的终端上什么都没有显示。