简体   繁体   English

Java Sockets + socket.io-client (Angular 13) = 连接失败和消息混乱

[英]Java Sockets + socket.io-client (Angular 13) = Failed connections and scrambled messages

I am trying to use a Java Socket Server with socket.io-client , but it has an erratic behavior from the moment of the connection.我正在尝试将 Java 套接字服务器与socket.io-client一起使用,但从连接那一刻起它就会出现不稳定的行为。 It manages to stablish connection, but then this exception is thrown in Angular:它设法建立连接,但随后在 Angular 中抛出此异常:

GET https://127.0.0.1:1532/socket.io/?EIO=4&transport=polling&t=N__rEfS net::ERR_SSL_PROTOCOL_ERROR

And the Server in Java only receives scrambled text repatedly而Java中的Server只会重复收到乱码在此处输入图像描述

and the Client starts connecting and disconnecting over and over again.并且客户端开始一遍又一遍地连接和断开连接。 Why is this happening?为什么会这样? Is there any way to get a cleaner Socket connection from Angular 13 to Java?有什么方法可以使从 Angular 13 到 Java 的 Socket 连接更清晰?

I use this Java Socket Server for many other applications and it works perfectly for everything else but this.我将这个 Java 套接字服务器用于许多其他应用程序,它适用于除此之外的所有其他应用程序。

This is the routine that reads the Java Server:这是读取 Java 服务器的例程:

void handleClientRequest() {
    try{
        mBufferIn = new BufferedReader(new InputStreamReader( socket.getInputStream()));
        mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

        //in this while the client listens for the messages sent by the server
        while (clientRun) {

            String clientMessage = mBufferIn.readLine();

            if (clientMessage != null && mMessageListener != null) {

                mMessageListener.messageReceived(clientMessage);
            }
        }
    } catch(Exception e){
        System.out.printf("%s: Unexpected client disconnection. Reason:%n", accountId);
        e.printStackTrace();
    }
}

And this is the Angular code:这是 Angular 代码:

this.socket = io('https://127.0.0.1:1532');

this.socket.on('connect', () => {
  const engine = this.socket.io.engine;
  console.log(engine.transport.name); // in most cases, prints "polling"

  engine.once('upgrade', () => {
    // called when the transport is upgraded (i.e. from HTTP long-polling to WebSocket)
    console.log(engine.transport.name); // in most cases, prints "websocket"
  });

  engine.on('packet', ({  }) => {
    // called for each packet received
  });

  engine.on('packetCreate', ({  }) => {
    // called for each packet sent
  });

  engine.on('drain', () => {
    // called when the write buffer is drained
  });

  engine.on('close', (reason: any) => {
    // called when the underlying connection is closed
  });
});

Code taken from https://socket.io/docs/v4/client-socket-instance/代码取自https://socket.io/docs/v4/client-socket-instance/

Socket IO is a communication protocol implemented on the top of websocket from my understanding (correct me if im wrong) you are using raw socket in java.套接字 IO 是在 websocket 之上实现的通信协议,根据我的理解(如果我错了请纠正我)你在 java 中使用原始套接字。

So very likely that "scrambled" text that you are receiving, is part of https handshake.您收到的“加扰”文本很可能是 https 握手的一部分。

My suggestion as way forward, will be to use library that handles websocket connections.作为前进的方向,我的建议是使用处理 websocket 连接的库。

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

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