Spring websockets without STOMP and SockJs, but with message broker and routing support

Is there a way to use the tools provided by Spring to connect to webSocket without using SockJS and STOMP? I need to implement the proprietary webSocket protocol, so I cannot use any of them at the moment.

I implemented / hacked the solution from the following answer , but it requires me to redefine things that I feel I should not worry about.

I think my question boils down to: is there a way to use nice features 2.(below), in particular MessageBroker, without using STOMP?

A bit of background:

I need to do the following:

  • Be able to embed a link to my persistence provider in each instance of the websocket handler
  • Handle each webSocket session in a separate instance of my handler object.
  • Resubmit the messages that I receive at one endpoint to multiple clients on another endpoint.
  • Implement your own protocol through a standard Text WebSocket connection.

I have tried so far:

  • Use javax.websocket where I can process each request in a separate instance. However, I need to iterate over open sessions and retransmit those that are associated with the correct endpoint. Also, I cannot use Spring to enter my continuity provider. This is what I have used so far, and it seems to be too hacked.

those. (Pseudo code)

@ServerEndpoint("/trade/{id}")
public String handleMessage(@PathParam(id) String id, webSocketSession session){
 if(id.equalsIgnoreCase("foo"){
      for(Session s: session.getOpenSessions(){
      //do things}
  } else if(id.equalsIgnoreCase("bar")){
      //do other things
  }
 }
}
  1. websocket Spring @MessageMapping("/trade") @SendTo("/queue/position-updates"). , , STOMP. , . , , .

.. (-)

@MessageMapping("/trade/{id}")
@SendTo("/trades/all")
public Greeting greeting(String message){
    //do things, return message
}
  1. websocket Spring, WebSocketConfigurer . . , Spring:
    • {id} registry.addHandler(WebsocketHandlerPool(), "/trade/{id}") WebsocketHandlerPool(), , WebsocketHandler. .
    • , websocket. ( ) @MessageMapping @SendTo.

.. ( :)

@Override
public void afterConnectionEstablished(WebSocketSession s){
    //parses the String of the Path URL and sets it as a field
    idFromSessionPath = getIdFromSessionPath(s);
}
@Override
public String handleTextMessage(WebSocketSession session, TextMessage message
{
    If(idFromSessionPath.equals("foo"){
     //do things
    } else {
     //do other things
    }
}

( )

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry  registry){
    registry.addHandler(threadPool(), "/trade/{id}");
}

@Bean
public WebSocketHandler threadPool(){
    return new PerConnectionWebSocketHandler(TradeStreamingHandler.class);

}
+4

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


All Articles