简体   繁体   English

未收到 Angular 8 socket.io-client 2.2.0 和 Java netty-socket.io 消息?

[英]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.我正在使用netty-socket.io并且我像演示一样实现了服务器。

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.我在服务器和客户端上都收到onConnect事件,但是当我发送消息{message: message}时,虽然我在网络选项卡中看到正在发送的消息,但我在服务器事件上没有收到任何信息。

  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.我的MessageEventObject具有String message属性、 constructor getterssetters ,看起来与客户端相同。

And this is my websocket service client-sided:这是我的 websocket 服务客户端:

  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.我很想在经过大量调试和头疼之后回答我自己的问题,我懒惰地将 Engine IO 与 tomcat 或 jetty 一起使用,并且只想使用那个不需要任何 servlet 的很棒的 netty 包,我试图修复它并弄清楚。

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.起初我以为这是客户端的协议版本,所以我使用了与演示在他们的github 页面上显示的完全相同的客户端,但这不起作用,所以问题是服务器端的。

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.除了其他构造函数之外,您的对象( MessageEventObject )似乎还必须有一个默认的空构造函数,这可能是因为 netty 尝试构建一个空对象并且它失败了,这会导致您看不到的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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