9

The following code is from spring mvc documentation:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/portfolio");
    }

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

@Controller
public class GreetingController {

    @MessageMapping("/greeting") {
    public String handle(String greeting) {
        return "[" + getTimestamp() + ": " + greeting;
    }
}

The client connects to http://localhost:8080/portfolio to establish WebSocket connection, I wonder what's the exact url of client sending request?

http://localhost:8080/portfolio/app

or

http://localhost:8080/app?

and in actual WebSocket frame, does the destination header contain relative url like /app, /topic or absolute url?

mzoz
  • 1,273
  • 1
  • 14
  • 28

1 Answers1

7

[Android] https://github.com/NaikSoftware/StompProtocolAndroid

[Spring] https://docs.spring.io/spring/docs/5.1.9.RELEASE/spring-framework-reference/web.html#websocket-stomp

Just set the end point by using

addEndpoint("/portfolio");

Use the following Url to connect to websocket

ws://localhost:8080/portfolio

But remember you have to connect to socket only once and after that just invoke the endpoints without URL. Beacause socket is streamline connection and you have to establish connection only once.

setApplicationDestinationPrefixes("/app");

Above line will set the end point /app using this you can only publish over the socket. However all who has subscribed to this topic will get notified.

enableSimpleBroker("/topic");

Broker are responsible for handling subscribe and publish for both as they listen and send data in dual way means publish and subscribe both unlike /app.

private var mStompClient: StompClient? = null
mStompClient = Stomp.over(Stomp.ConnectionProvider.OKHTTP, "ws://localhost:8080/portfolio")

Connect to websocket using the above line. since we have to connect to socket end point only once write this in singleton.

val response = stomp.topic("/topic")
.subscribe { topicMessage -> }

Now above line will subscribe to your socket client means anytime you pushed the data from /topic this will this response variable will notified.

stompClient.send(StompMessage(StompCommand.SEND,
    listOf(StompHeader(StompHeader.DESTINATION, "/topic")),
    gson.toJson(myDataModel)))?
.subscribe()

Using above line you will you will you will send data to the socket which is specified as /topic.

@MessageMapping("/action")
fun performDeviceAction(@Payload myDataModel: MyDataModel) {}

Use the above line to receive the data from client on socket /action

public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new SocketTextHandler(), "/user");
    }

In order to tell Spring to forward client requests to the endpoint , we need to register the handler. Above snipplet will register a client.

Use below link and download source code for more information https://www.javainuse.com/spring/boot-websocket

Kaushik Burkule
  • 816
  • 1
  • 12
  • 27
  • I use kotlin for both spring boot and android development but i think it will be no problem to understand the flow also i am also improving my answer over time. – Kaushik Burkule Aug 29 '19 at 06:58
  • Hi buddy thx for your time but you didn't give direct answer to my question? I'm already familiar with all the messaging process, looking forward to more of your explanation! – mzoz Aug 29 '19 at 14:52
  • I am writing this is answer from android developer perspective as of this explanation is all about communicating over socket between android and web server. I will update my answer according to your question but can you tell me what exactly are you expecting? – Kaushik Burkule Aug 29 '19 at 17:31
  • For example in your code `@MessageMapping("/action")` the `/action` a relative path, I wonder what's the absolute path? `ws://localhost:8080/portfolio/action` or `ws://localhost:8080/action`? – mzoz Aug 29 '19 at 23:15
  • 2
    ws://localhost:8080/portfolio use this path just to connect to socket and after that for every communication just use /topic for communication. you dont have to put full URL every time. I have updated answer just checkout once. – Kaushik Burkule Aug 30 '19 at 07:24