I have a WebSocket server that written in Java and started without any problem. (on "ws" endpoint)
Simply, I want to develop a client in below tasks :
When I used Stomp as below it works well. but I couldn't do this way when I use StandardWebSocketClient.
//Init
WebSocketClient client = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(client);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
StompSessionHandler sessionHandler = new CustomStompSessionHandler();
//Connect
StompSession stompSession = stompClient.connect("ws://localhost:8080/ws", sessionHandler).get();
//Subscribe
stompSession.subscribe("/topic/public", sessionHandler);
//Send Message
stompSession.send("/app/chat.addUser", new ChatMessage(MessageType.JOIN, "New User Added"));
stompSession.send("/app/chat.sendMessage", new ChatMessage(MessageType.CHAT, "Test Message", "Test username"));
Now I want to do this with SockJsClient. it's pretty easy to handshake and create a connection but after that, how can I subscribe to the specific topic?
//Init
List<Transport> transports = new ArrayList<>(2);
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
transports.add(new RestTemplateXhrTransport());
SockJsClient sockJsClient = new SockJsClient(transports);
//Connect (Handshake)
WebSocketSession webSocketSession = sockJsClient.doHandshake(new CustomWebSocketHandler(), "ws://localhost:8080/ws").get();
//HOW CAN I SUBSCRIBE ?????
//Send Message (To where? Which Path? Which topic)
WebSocketMessage webSocketMessage = new TextMessage(new String("test Message").getBytes());
webSocketSession.sendMessage(webSocketMessage);
In BRIEF: HOW CAN I SUBSCRIBE TO SPECIFIC TOPIC WHEN I USE SockJsClient?
To remind: Server was ok, and the other approach (StandardWebSocketClient) was working well.
If you want to implement STOMP protocol from scratch then you need to read specification and construct your SUBSCRIBE
and other commands by hand in text message body.
But you can initialize the same WebSocketStompClient
with configured SockJsClient
as WebSocketClient
constructor argument like this