1
votes

I am using the following project to build a socket.io server: https://github.com/mrniko/netty-socketio

My server:

public class ServerTest {
    public static void main(String... args) {
        Configuration config = new Configuration();
        config.setPort(8080);

        config.setAuthorizationListener(new AuthorizationListener() {
            public boolean isAuthorized(HandshakeData handshakeData) {
                System.out.println("yes!" + handshakeData.getAddress());
                return true;
            }
        });
        final SocketIOServer server = new SocketIOServer(config);

        server.addEventListener("message", String.class, new DataListener<String>() {

            public void onData(SocketIOClient socketIOClient, String s, AckRequest ackRequest) throws Exception {
                System.out.println("hey");
            }
        });

        server.start();
    }
}

I accept all connections, I have tested the connection like this with socket.io javascript client:

var ws = io.connect('localhost:8080');
ws.on('connect', function() {
    ws.emit('message', "I am a message!");
});

Now, I get the print "hey", but what if I send a data object like this:

ws.emit('message', {data: "I am a message!"});

How can I decode this message? Is there a way to debug what object type is being sent to the server?

1

1 Answers

0
votes

You are sending an Object from client to server. On the server you need to create an object which has a string field named data. See the following demo class, basically now you can receive the object on the server using code like
server.addEventListener("ackevent1", ChatObject.class, new DataListener<ChatObject>() { @Override public void onData(final SocketIOClient client, ChatObject data, final AckRequest ackRequest) { ...