tyrus icon indicating copy to clipboard operation
tyrus copied to clipboard

Can't use lambda expression for MessageHandler

Open sco0ter opened this issue 8 years ago • 1 comments

Neither of the following two lambda expression work, when registering a PongMessage handler.

session.addMessageHandler((MessageHandler.Whole<PongMessage>) message -> {
});
session.addMessageHandler(PongMessage.class, message -> {
});

The reason is, that the internal method static Class<?> getHandlerType(MessageHandler handler) returns Object instead of PongMessage for the type.

The workaround for this issue is to not using lambdas, but implement the interface on a class and use that:

class PongHandler implements MessageHandler.Whole<PongMessage>

sco0ter avatar Apr 04 '18 09:04 sco0ter

I've just come across this one. It looks to be caused by the type erasure of the lambda expression as TyrusSession#notifyPongHandler is trying to infer the type rather than getting it explicitly from the MessageHandlerManager#registeredHandlers. As PongMessage can only use MessageHandler.Whole types, I think this can be replace with getMessageHandler(Class). I'll submit a patch when I get a few minutes.

dansiviter avatar Jun 28 '20 18:06 dansiviter