kartik
kartik

Reputation: 168

Jetty WebSockets onBinaryMessage function not being called

I am embedding Jetty (version - 9.4.1.v20170120) websockets api in my application and implemented the following two classes for it


    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.websocket.server.WebSocketHandler;
    import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;

    public class WebSocketTest{
        private static final int MAX_MESSAGE_SIZE = 3000000;

        public static void main(String[] args) throws Exception {
            Server server = new Server(9999);
            WebSocketHandler wsHandler = new WebSocketHandler() {
                @Override
                public void configure(WebSocketServletFactory factory) {

                  factory.getPolicy().setMaxBinaryMessageSize(MAX_MESSAGE_SIZE);
                factory.register(MyWebSocketHandler.class);

            }
        };
        server.setHandler(wsHandler);
        server.start();
        server.join();
    }
}

And

import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.*;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;

import java.io.File;
import java.io.IOException;

@WebSocket
public class MyWebSocketHandler  {
    private Session session;

    @OnWebSocketClose
    public void onClose(int statusCode, String reason) {
        System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
    }

    @OnWebSocketError
    public void onError(Throwable t) {
        System.out.println("Error: " + t.getMessage());
    }

    @OnWebSocketConnect
    public void onConnect(Session session) {
        this.session = session;
        System.out.println("Connect: " + session.getRemoteAddress().getAddress());
        try {
            session.getRemote().sendString("Hello Webbrowser");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void onBinaryMessage(byte[] data, int offset, int length) throws IOException {
        System.out.println("Wriiten");
        FileUtils.writeByteArrayToFile(new File(System.nanoTime()+"hello.jpg"), data);
        try {
            this.session.getRemote().sendString("Receiving Binary Data==>"+data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnWebSocketMessage
    public void onMessage(String message) {
        System.out.println("String Message ====> " + message);
        try {
            this.session.getRemote().sendString("Receiving String Data==>"+message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

but the onBinaryMessage function is not getting called when I send bytes using an android app with AndroidAsync , in fact I even logged the Message type using onFrame method but I was getting messages of only 'TEXT' type and no binary types.So I am getting string but not bytes . What to do???I also checked other stackoverflow posts but none had the answer?

Upvotes: 0

Views: 490

Answers (1)

kartik
kartik

Reputation: 168

just got it working by using method overloading here (found it in some post online after a lot of searching ... jetty websocket documentation and javadoc is pretty bad)


@OnWebSocketMessage
    public void onMessage(byte[] data, int offset, int length) throws IOException {
        System.out.println("Wriiten");
        FileUtils.writeByteArrayToFile(new File(System.nanoTime()+"hello.jpg"), data);
        try {
            this.session.getRemote().sendString("Receiving Binary Data==>"+data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

so now MyWebSocketHandler.java looks like


import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.*;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;

import java.io.File;
import java.io.IOException;

@WebSocket
public class MyWebSocketHandler  {
    private Session session;

    @OnWebSocketClose
    public void onClose(int statusCode, String reason) {
        System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
    }

    @OnWebSocketError
    public void onError(Throwable t) {
        System.out.println("Error: " + t.getMessage());
    }

    @OnWebSocketConnect
    public void onConnect(Session session) {
        this.session = session;
        System.out.println("Connect: " + session.getRemoteAddress().getAddress());
        try {
            session.getRemote().sendString("Hello Webbrowser");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnWebSocketMessage
    public void onMessage(byte[] data, int offset, int length) throws IOException {
        System.out.println("Wriiten");
        FileUtils.writeByteArrayToFile(new File(System.nanoTime()+"hello.jpg"), data);
        try {
            this.session.getRemote().sendString("Receiving Binary Data==>"+data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnWebSocketMessage
    public void onMessage(String message) {
        System.out.println("String Message ====> " + message);
        try {
            this.session.getRemote().sendString("Receiving String Data==>"+message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Although I got the answer , I still want to post it as it may help someone else!! as I couldn't find it through stack overflow

Upvotes: 1

Related Questions