skip to Main Content

I’ve got the simple WebSocket example from https://spring.io/guides/gs/messaging-stomp-websocket/

package com.example.chatservice.message;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gs-guide-websocket").withSockJS();
    }
}
@Controller
public class ChatController {
    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        return new Greeting("Hello from chat!");
    }
}

and I’d like to receive the Greeting message from the spring server to an android app. I use https://github.com/NaikSoftware/StompProtocolAndroid as a stomp client

package com.example.chatapp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import org.java_websocket.WebSocket;

import ua.naiksoftware.stomp.Stomp;
import ua.naiksoftware.stomp.client.StompClient;

public class MainActivity extends AppCompatActivity {
    private StompClient mStompClient;
    public static  final String TAG="StompClient";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button view = (Button) findViewById(R.id.send_message);
        view.setOnClickListener(e->  new StompTask().execute(""));
    }

    private static class StompTask extends AsyncTask<String, Void, String> {
        private StompClient mStompClient;
        String TAG="LongOperation";

        @Override
        protected String doInBackground(String... params) {
            mStompClient = Stomp.over(WebSocket.class, "http://10.0.2.2:8080/gs-guide-websocket/websocket");
            mStompClient.connect();

            mStompClient.topic("/topic/greetings").subscribe(topicMessage -> {
                Log.d(TAG, topicMessage.getPayload());
            });

            mStompClient.lifecycle().subscribe(lifecycleEvent -> {
                switch (lifecycleEvent.getType()) {

                    case OPENED:
                        Log.d(TAG, "Stomp connection opened");
                        break;

                    case ERROR:
                        Log.e(TAG, "Error", lifecycleEvent.getException());
                        break;

                    case CLOSED:
                        Log.d(TAG, "Stomp connection closed");
                        break;
                }
            });

            return "Executed";
        }
    }
}

and the connection to the

http://10.0.2.2:8080/gs-guide-websocket/websocket

works, because the log shows it’s connected

2022-01-18 14:04:56.486 23717-23752/com.example.chatapp D/LongOperation: Stomp connection opened

but it does not subscribe to the topic:

mStompClient.topic("/topic/greetings").subscribe(topicMessage -> {
    Log.d(TAG, topicMessage.getPayload());
});

because when I set the breakpoint it doesn’t ever hit it.

how can I receive this message?

2

Answers


  1. I think everything looks awesome except "http://10.0.2.2:8080/gs-guide-websocket/websocket" should probably be "ws://10.0.2.2:8080/gs-guide-websocket/websocket" for the websocket.

    The author of the librabry put this in their example code:

    `mStompClient = Stomp.over(Stomp.ConnectionProvider.OKHTTP, "ws://" + ANDROID_EMULATOR_LOCALHOST + ":" + RestClient.SERVER_PORT + "/example-endpoint/websocket");` 
    
    Login or Signup to reply.
  2. First of all I suppose you understand that if you don’t send the proper message to the specified request topic, then you won’t get anything here –

    mStompClient.topic("/topic/greetings").subscribe(topicMessage -> {
        Log.d(TAG, topicMessage.getPayload());
    });
    

    Today I had the issue where when sending a message,

    mStompClient.send(REQUEST_TOPIC, requestAsString)
    

    I also did not receive anything in the subscription. The problem is that in addition to subscribing to the topic of receiving messages, you must subscribe to the topic of sending messages –

    mStompClient.send(REQUEST_TOPIC, requestAsString)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(
                                    {
                                    //onComplete
                                    }, { error ->
                                      error.safeLog()
                                    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search