我正在嘗試將 Java 套接字服務器與socket.io-client. 它設法建立連接,但隨后在 Angular 中引發了此例外:
GET https://127.0.0.1:1532/socket.io/?EIO=4&transport=polling&t=N__rEfS net::ERR_SSL_PROTOCOL_ERROR
并且Java中的服務器只重復接收加擾的文本

客戶端開始一遍又一遍地連接和斷開連接。為什么會這樣?有什么方法可以讓 Angular 13 到 Java 的 Socket 連接更干凈?
我將此 Java Socket 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();
}
}
這是 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
});
});
代碼取自https://socket.io/docs/v4/client-socket-instance/
uj5u.com熱心網友回復:
據我了解,Socket IO 是在 websocket 之上實作的通信協議(如果我錯了,請糾正我)你在 java 中使用原始套接字。
因此,您收到的“加擾”文本很可能是 https 握手的一部分。
我的建議是使用處理 websocket 連接的庫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/457317.html
上一篇:為什么相等的磁區資料作業得更快?
