0

I've the same problem as this Android Emitter.Listener not working question but the given answer in that question was not helpful and I couldn't even find any solution .Can anyone please help me to solve this?

Here in my code I'm sending base64 string to node.js server which saves the file and sending the link back to me (The file saving is working fine).Even the link is not toasted.

My Code is:

public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
socket.connect();
    socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            socket.on("data", handleIncomingMessage);
        }
    });}
//Onclick of a button
JSONObject obj=new JSONObject();
            try {
                obji.put("profile_pic",image);
                socket.emit("data",obji);
            } catch (JSONException e) {
                e.printStackTrace();
            }
    private Emitter.Listener handleIncomingMessage = new Emitter.Listener(){

    @Override
    public void call(final Object... args){
        Toast.makeText(getActivity().getApplicationContext(),"Hello India",Toast.LENGTH_LONG).show();
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                JSONObject data = (JSONObject) args[0];
                //JSONObject d=(JSONObject) args[1];
                String imgLink;
                try {
                    imgLink = data.getString("data").toString();
                    Glide.with(getContext()).load(imgLink).asBitmap().dontAnimate().into(imageView);

                    Toast.makeText(getActivity().getApplicationContext(),imgLink,Toast.LENGTH_LONG).show();


                } catch (JSONException e) {
Toast.makeText(getActivity().getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
                }

            }
        });
    }
};

1 Answers1

0

I think I can. You see you do connect to your server but that connection takes a little while and before that happens you immediately set the onSendFile message handler to the socket instance. You should ideally set the message handlers AFTER the connection is successfully established with the server.

Also, you should emit your own messages with socket.emit AFTER the above said event. To do this simply write:

mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {

                mSocket.on("send file", onSendFile);
            }
        });
mSocket.connect();

This way the handler gets added to the instance after the EVENT_CONNECT takes place that is, after the connection to the server is established.

Sidenote: Also, don't forget to remove the handler from the socket instance when the activity is destroyed. Call mSocket.off("send file", onSendFile) inside the onDestroy method of the activity. This way, any events from the server won't crash your app if the code inside onSendFile uses any of the UI components of the activity.
Cheers!

Mustansir Zia
  • 984
  • 10
  • 18