错误:关闭浏览器/选项卡时在 TLSWrap.onStreamRead 处读取 ECONNRESET

hre*_*tic 5 javascript node.js express socket.io express.io

我在nodejs中编写我的第一个聊天应用程序,我的应用程序基本上跟踪在线用户并将他们放在房间中通过webrtc聊天...我使用pm2来运行聊天服务器

所以我注意到有时当我关闭浏览器选项卡时我的应用程序会崩溃并且 pm2 将重新加载服务器所以我检查了日志并看到了这个错误

0|chat  | Error: read ECONNRESET
0|chat  |     at TLSWrap.onStreamRead (node:internal/stream_base_commons:220:20) {
0|chat  |   errno: -104,
0|chat  |   code: 'ECONNRESET',
0|chat  |   syscall: 'read'
0|chat  | }
Run Code Online (Sandbox Code Playgroud)

这是在没有 pm2 的情况下运行时的错误

node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:220:20)
Emitted 'error' event on TLSSocket instance at:
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -104,
  code: 'ECONNRESET',
  syscall: 'read'
}
Run Code Online (Sandbox Code Playgroud)

为了确保不是我的代码导致此错误,我删除了应用程序中的所有代码,只放置了 2 个简单的函数来监听在线(由在线客户端通过 socket.io 调用)和断开连接(由用户自动断开连接)事件。 .果然,在关闭并打开一些选项卡后,它再次发生,这是我的代码的简化版本

我使用的Express.io是 Express 和 Socket.io 的组合

const  env = require('dotenv').config({ path: '.env' })
const https = require('https');
const fs = require('fs');

app = require('express.io')();

var options = {
    key: fs.readFileSync( env.parsed.SSL_KEY),
    cert: fs.readFileSync(env.parsed.SSL_CERT)
};
app.https(options).io();
console.log('|-> protocol : https ');



app.io.route('online' , function (req){
    console.log(`| online socket -> ${req.socket.id}`);
})


app.io.route('disconnect', function(req) {
    console.log(`|->****************** disconnect : ${req.socket.id}`);
});

var PORT = env.parsed.PORT  || 8080
console.log(`|-> listining to port ${PORT} `);
app.listen(PORT );
Run Code Online (Sandbox Code Playgroud)

我已经在网上搜索并知道为什么会发生(我认为),但我在这段代码中没有任何数据通信......我不知道如何处理这个问题并感谢任何帮助

这是完整的日志,您可以在关闭一些选项卡后看到它发生的情况

在此输入图像描述

/root/.pm2/logs/chat-out.log last 15 lines:
0|chat     | |-> protocol : https
0|chat     | |-> listining to port 8080

/root/.pm2/logs/chat-error.log last 15 lines:
0|chat     | (node:19821) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
0|chat     | (Use `node --trace-deprecation ...` to show where the warning was created)

0|chat  | | online socket -> Z2z7m6D9OXKPGRVhbz4R
0|chat  | |->****************** disconnect : H_1-d7sIm2oIvxzzbz4Q
0|chat  | | online socket -> JrnLWbqN4luBtYg4bz4S
0|chat  | | online socket -> ZSY1wIywMEdmUcALbz4T
0|chat  | | online socket -> ugSg08DksJ73ld1Rbz4U
0|chat  | |->****************** disconnect : ugSg08DksJ73ld1Rbz4U
0|chat  | |->****************** disconnect : ZSY1wIywMEdmUcALbz4T
0|chat  | Error: read ECONNRESET
0|chat  |     at TLSWrap.onStreamRead (node:internal/stream_base_commons:220:20) {
0|chat  |   errno: -104,
0|chat  |   code: 'ECONNRESET',
0|chat  |   syscall: 'read'
0|chat  | }
PM2     | App [chat:0] exited with code [1] via signal [SIGINT]
PM2     | App [chat:0] starting in -fork mode-
PM2     | App [chat:0] online
0|chat  | |-> protocol : https
0|chat  | |-> listining to port 8080
0|chat  | (node:19864) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
0|chat  | (Use `node --trace-deprecation ...` to show where the warning was created)
Run Code Online (Sandbox Code Playgroud)

Mar*_*ari 0

在不查看客户端代码的情况下很难找出问题,但我的赌注是声明这条路由

app.io.route('disconnect', function(req) {
    console.log(`|->****************** disconnect : ${req.socket.id}`);
});
Run Code Online (Sandbox Code Playgroud)

你正在滥用express.io图书馆:

https://github.com/techpines/express.io/tree/master/lib#reserved-events

这些事件是保留的,除非您知道自己在做什么app.io.route,否则不应与它们一起使用。req.io.on

  • 连接
  • 连接
  • 断开
  • 连接失败
  • 错误
  • 信息
  • 重新连接失败
  • 重新连接
  • 重新连接

顺便说一句,就像另一条评论所说,这个库很旧,你不应该使用它。它使用Socket.IO v0.9,当前版本是v4。