简体   繁体   中英

Angular 8 socket.io-client 2.2.0 and Java netty-socket.io message not being received?

I am using netty-socket.io and I implemented the server like the demo.

I receive onConnect event both on server and client, but when I sent a message {message: message} I don't get anything on the server event though I see the message being sent in the network tab.

  Configuration config = new Configuration();
    config.setHostname("localhost");
    config.setPort(9092);

    final SocketIOServer server = new SocketIOServer(config);
    server.addConnectListener(socketIOClient -> System.out.println("Connection test"));
    server.addEventListener("messageevent", MessageEventObject.class, new DataListener<MessageEventObject>() {
        @Override
        public void onData(SocketIOClient socketIOClient, MessageEventObject messageEventObject, AckRequest ackRequest) throws Exception {
            System.out.println("message received!");
        }
    });
    server.start();

My MessageEventObject has String message property, constructor getters and setters , looking the same as client-sided.

And this is my websocket service client-sided:

  export class WebsocketService {

   private socket;
   private subject = new Subject < any > ();

   constructor() {
    console.log('test!');
   }

   public connect(host: string, port: number) {
    this.socket = io(`http://${host}:${port}`, {
     'reconnection': false
    });
    this.socket.on('connect', this.onConnected);
    this.socket.on('connect_error', this.onConnectionFailure);
   }

   public getConnectionStateUpdate(): Observable < any > {
    return this.subject.asObservable();
   }

   public sendMessage(message: string) {
    console.log('test');
    this.socket.emit('messageevent', {
     message: message
    });
   }

   private onConnected = () => {
    this.subject.next({
     connected: true
    });
   }

   private onConnectionFailure = () => {
    this.subject.next({
     connected: false
    });
   }
  }

Is there anything that I did wrong?

I would love to answer my own question after tons of debugging and breaking my head, my laziness to use Engine IO with tomcat or jetty, and just wanting to use that awesome netty package which does not require any servlets, I tried to fix it and figure out.

At first I thought it was the client's protocol version, so I used the exact same client as the demo shows on their github page here but that didn't work so the problem is server-sided.

It appears that your object ( MessageEventObject ) must have a default empty constructor aswell in addition to your other constructors, probably because netty tries to build an empty object and it fails which causes an exception that you don't see.

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