Websockets Next and streams with different message types #49107
Replies: 2 comments
-
\CC @mkouba |
Beta Was this translation helpful? Give feedback.
-
Yes, an endpoint may declare at most one
So if I understand it correctly you would like to keep the If you find the @WebSocketClient(path = "/streams")
public class StreamsEndpoint {
@OnTextMessage
void onMessage(MiniTicker[] miniTickers) {
Log.info("onMessage(MiniTicker): " + miniTickers.length);
}
@OnError
void onDeserializationError(MiniTickerDeserializationError e) {
Log.infof("onError, control message=%s", e.getText());
}
}
public class MiniTickerDeserializationError extends Exception {
// ...
}
@Singleton
@Priority(1) // use a higher priority then the built-int JsonTextMessageCodec
public class MiniTickerCodec implements TextMessageCodec<MiniTicker[]> {
@Inject
ObjectMapper mapper;
@Override
public boolean supports(Type type) {
if (type instanceof Class clazz) {
return clazz.componentType().equals(MiniTicker.class);
}
return false;
}
@Override
public String encode(MiniTicker[] value) {
try {
return mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new IllegalStateException(e);
}
}
@Override
public MiniTicker[] decode(Type type, String value) {
try {
return mapper.readValue(value, mapper.getTypeFactory().constructType(type));
} catch (Exception e) {
throw new MiniTickerDeserializationError(value, e);
}
}
} Keep in mind that you can declare multiple |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I happened across this websocket streams service:
https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md
and wanted to try hooking up the Quarkus websockets as their java library is a bit clunky. I'm able to do this, but I cannot deal with the initial response message to the subscribe attempt, or the response message if one unsubscribes as cleanly as I would like. I was hoping I could deal with this by having a generic text message callback along with the a callback with the expected unmarshalled type along the lines of:
This is not allowed. So instead I need to add an error callback and check the exception type:
I would rather not have to be checking against an exception type that does not seem a reliable api type. I have seen these types of streaming services be very lax in what they put in even the expected object messages, so deserialization issues are a common occurrence.
What if instead of using the
onError
method or a general@OnTextMessage
handler, the webservices framework supported a@OnSerializationError
callback to distinguish from the expected text message type and a connection level error?Beta Was this translation helpful? Give feedback.
All reactions