Zeromq PUB SUB Does not receive messages (Golang as an Android server as a client)

I am running a Zeromq server server in the Golang (PUB). I am testing these functions with a basic example. My server is in the Digitalocen cassette. His work is wonderful. And I can connect and receive a message on my local machine through the Golang client as SUB, but when I try to use the same logic in android, I get nothing. He was stuck in a loop. And no mistakes.

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

            }
        }
    }

}

I realized that I can not go this line. I could not find the problem.

 String string = new String(subscriber.recv(0));
+4
source share

Source: https://habr.com/ru/post/1614759/


All Articles