我在Golang(PUB)中运行My Server Side Zeromq。我正在使用提供的基本示例测试这些功能。我的服务器在Digitalocen Droplet中。它运行良好。我能够通过Golang客户端作为SUB在我的本地机器上连接和接收消息,但是当我在android中尝试相同的逻辑时,我什么也得不到。它停留在循环中。并且没有错误。
服务器端PUB:Golang
//
// Pubsub envelope publisher.
//
package main
import (
zmq "github.com/pebbe/zmq4"
"time"
)
func main() {
// Prepare our publisher
publisher, _ := zmq.NewSocket(zmq.PUB)
defer publisher.Close()
publisher.Bind("tcp://*:5563")
for {
// Write two messages, each with an envelope and content
publisher.Send("A", zmq.SNDMORE)
publisher.Send("We don't want to see this", 0)
publisher.Send("B", zmq.SNDMORE)
publisher.Send("We would like to see this", 0)
time.Sleep(time.Second)
}
}
客户端SUB:Android
我也有互联网许可
package com.example.fahim.zeromq;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.zeromq.ZMQ;
public class MainActivity extends AppCompatActivity {
TextView output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
new Zconnect().execute();
}
private class Zconnect extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
subscriber.connect("tcp://server-ip:5563");
subscriber.subscribe("A".getBytes());
while (true) {
String string = new String(subscriber.recv(0));
Log.i("Message :", string);
Log.i("Info :", "This line never shows.");
}
}
}
}
我意识到它无法通过这条线。我找不到问题。
String string = new String(subscriber.recv(0));