3

我在 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  | }

这是在没有 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'
}

为了确保不是我的代码导致了这个错误,我删除了我的应用程序中的所有代码,只放了 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 );

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

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

在此处输入图像描述

/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)
4

1 回答 1

0

没有看到客户端代码很难找出问题,但我敢打赌是通过声明这条路线

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

你在滥用express.io图书馆:

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

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

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

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

于 2022-02-28T01:26:22.647 回答