Finally I have my websocket client connecting to the endpoint but I can't extract message payload. I can get headers but payload is not recognized.
My WebSocket client looks like below:
WebSocketTransport webSocketTransport = new WebSocketTransport(standardWebSocketClient);
SockJsClient sockJsClient = new SockJsClient(Arrays.asList(webSocketTransport));
WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
stompClient.setMessageConverter(new StringMessageConverter());
StompSessionHandler sessionHandler = new MyStompSessionHandler();
ListenableFuture<StompSession> connect = stompClient.connect(URL, sessionHandler);
StompSession stompSession = connect.get();
System.out.println("sessionId: " + stompSession.getSessionId());
String path = "/queue/orders";
stompSession.subscribe(path, new MySimpleStompFrameHandler());
Stomp Frame Handler:
private class MySimpleStompFrameHandler implements StompFrameHandler {
@Override
public Type getPayloadType(StompHeaders stompHeaders) {
System.out.println("Headers " + stompHeaders.toString());
return String.class;
}
@Override
public void handleFrame(StompHeaders stompHeaders, Object payload) {
System.out.println("Msg " + payload.toString());
completableFuture.complete(payload.toString());
}
}
I have headers written in the terminal but there is nothing from handleFrame method. Any idea ?
Edited: After debugging i see that the problem is in DefaultStompSession class which uses my implementation of frameHandler
private void invokeHandler(StompFrameHandler handler, Message<byte[]> message, StompHeaders stompHeaders) {
if (message.getPayload().length == 0) {
handler.handleFrame(stompHeaders, null);
return;
}
Type type = handler.getPayloadType(stompHeaders);
Class<?> payloadType = ResolvableType.forType(type).resolve();
Object object = getMessageConverter().fromMessage(message, payloadType);
if (object == null) {
throw new MessageConversionException("No suitable converter, payloadType=" + payloadType +
", handlerType=" + handler.getClass());
}
handler.handleFrame(stompHeaders, object);
}
Problem is after this line Type type = handler.getPayloadType(stompHeaders);
After that nothing more is executed and my program just ends so handleFrame() is not even executed.
What is wrong there ? Maybe with Type returned from getPayloadType - I choose String because I think every message can be presented as string.
I had a similar issue.
The problem was that I had specified stompClient.setMessageConverter(new StringMessageConverter());
, while a JSON was passed as payload.
Solution (for my issue): stompClient.setMessageConverter(new MappingJackson2MessageConverter());
What you can do to find a solution:
Throw (or log) when an exception is triggered. For your case, you should add an override for the following method in MyStompSessionHandler
:
@Override
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
throw new RuntimeException("Failure in WebSocket handling", exception);
}
While debugging invokeHandler
, check the content of the payload. If you are able to execute the code, you can do this by executing new String((byte[]) message.getPayload())
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With