简体   繁体   中英

netty-socketio read socket.io data?

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?

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) { ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM