dpi10335 2015-11-06 14:47
浏览 241

Zeromq PUB SUB不接收消息(Golang作为服务器,Android作为客户端)

I am running My Server Side Zeromq in Golang (PUB) . I am testing those features with the basic example provided. My Server is in Digitalocen Droplet . Its running fine . And I am able to connect and receive message on my local machine via Golang client as SUB but when I try the same logic in android then I get nothing . It stuck in the loop . And No errors.

Server Side 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)
    }
}

Client Side SUB : Android

I also have the Internet Permission

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.");

            }
        }
    }

}

What I came to realize that it can not get passed this line . I could not find the problem .

 String string = new String(subscriber.recv(0));
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 易康econgnition精度验证
    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败