这里我们将给出一个subscribe模式的客户端示例程序,如下:
private class ZeroMQMessageTask extends AsyncTask<String, Void, String> {
private String message = "";
public ZeroMQMessageTask() {
}
@Override
protected String doInBackground(String... params) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
subscriber.subscribe("");
subscriber.connect("tcp://x.x.x.x:6666"); // 注意,这里必须是服务器的IP地址或DNS Name
message = new String(subscriber.recv(0));
subscriber.close();
context.term();
return message;
}
@Override
protected void onPostExecute(String result) {
EditText text = (EditText) myView.findViewById(R.id.message); // 这里替换成对应的UI控件用于显示结果
searchEditText.setText(result);
}
}
相关阅读:
在Ubuntu 11.04上安装ZeroMQ