use of javax.websocket.MessageHandler in project jetty.project by eclipse.
the class JsrEndpointEventDriver method onBinaryFrame.
@Override
public void onBinaryFrame(ByteBuffer buffer, boolean fin) throws IOException {
if (activeMessage == null) {
final MessageHandlerWrapper wrapper = jsrsession.getMessageHandlerWrapper(MessageType.BINARY);
if (wrapper == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("No BINARY MessageHandler declared");
}
return;
}
if (wrapper.wantsPartialMessages()) {
activeMessage = new BinaryPartialMessage(wrapper);
} else if (wrapper.wantsStreams()) {
final MessageInputStream stream = new MessageInputStream();
activeMessage = stream;
dispatch(new Runnable() {
@SuppressWarnings("unchecked")
@Override
public void run() {
MessageHandler.Whole<InputStream> handler = (Whole<InputStream>) wrapper.getHandler();
handler.onMessage(stream);
}
});
} else {
activeMessage = new BinaryWholeMessage(this, wrapper);
}
}
activeMessage.appendFrame(buffer, fin);
if (fin) {
activeMessage.messageComplete();
activeMessage = null;
}
}
use of javax.websocket.MessageHandler in project jetty.project by eclipse.
the class JsrSessionTest method testMessageHandlerReplaceTextHandler.
@Test
public void testMessageHandlerReplaceTextHandler() throws DeploymentException {
MessageHandler oldText = new StringWholeHandler();
// add a TEXT handler
session.addMessageHandler(oldText);
// add BINARY handler
session.addMessageHandler(new ByteArrayWholeHandler());
// remove original TEXT handler
session.removeMessageHandler(oldText);
// add new TEXT handler
session.addMessageHandler(new LongMessageHandler());
MessageHandlerWrapper wrapper = session.getMessageHandlerWrapper(MessageType.BINARY);
Assert.assertThat("Binary Handler", wrapper.getHandler(), instanceOf(ByteArrayWholeHandler.class));
Assert.assertEquals("Message Class", wrapper.getMetadata().getMessageClass(), byte[].class);
wrapper = session.getMessageHandlerWrapper(MessageType.TEXT);
Assert.assertThat("Text Handler", wrapper.getHandler(), instanceOf(LongMessageHandler.class));
Assert.assertEquals("Message Class", wrapper.getMetadata().getMessageClass(), Long.class);
}
Aggregations