這裡我們將給出一個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);
}
}
關於創建ZeroMQ的服務端,請參考這篇文章: http://www.linuxidc.com/Linux/2015-01/112315.htm