Search in sources :

Example 6 with MessageHandler

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;
    }
}
Also used : MessageHandlerWrapper(org.eclipse.jetty.websocket.jsr356.MessageHandlerWrapper) BinaryWholeMessage(org.eclipse.jetty.websocket.jsr356.messages.BinaryWholeMessage) MessageInputStream(org.eclipse.jetty.websocket.common.message.MessageInputStream) MessageHandler(javax.websocket.MessageHandler) BinaryPartialMessage(org.eclipse.jetty.websocket.jsr356.messages.BinaryPartialMessage) MessageInputStream(org.eclipse.jetty.websocket.common.message.MessageInputStream) InputStream(java.io.InputStream) Whole(javax.websocket.MessageHandler.Whole)

Example 7 with MessageHandler

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);
}
Also used : StringWholeHandler(org.eclipse.jetty.websocket.jsr356.handlers.StringWholeHandler) LongMessageHandler(org.eclipse.jetty.websocket.jsr356.handlers.LongMessageHandler) MessageHandler(javax.websocket.MessageHandler) ByteArrayWholeHandler(org.eclipse.jetty.websocket.jsr356.handlers.ByteArrayWholeHandler) LongMessageHandler(org.eclipse.jetty.websocket.jsr356.handlers.LongMessageHandler) Test(org.junit.Test)

Aggregations

MessageHandler (javax.websocket.MessageHandler)7 Whole (javax.websocket.MessageHandler.Whole)2 MessageInputStream (org.eclipse.jetty.websocket.common.message.MessageInputStream)2 MessageHandlerWrapper (org.eclipse.jetty.websocket.jsr356.MessageHandlerWrapper)2 Test (org.junit.Test)2 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)1 Session (javax.websocket.Session)1 WebSocketContainer (javax.websocket.WebSocketContainer)1 Context (org.apache.catalina.Context)1 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)1 Tomcat (org.apache.catalina.startup.Tomcat)1 SleepingText (org.apache.tomcat.websocket.TesterMessageCountClient.SleepingText)1 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)1 MessageReader (org.eclipse.jetty.websocket.common.message.MessageReader)1 ByteArrayWholeHandler (org.eclipse.jetty.websocket.jsr356.handlers.ByteArrayWholeHandler)1