Issue
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?
Solution
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");`
Answered By - ferrouskid
Answer Checked By - David Marino (JavaFixing Volunteer)