继续获取和发布 /socket.io/?EIO=3&transport=polling&t=
Posted
技术标签:
【中文标题】继续获取和发布 /socket.io/?EIO=3&transport=polling&t=【英文标题】:Keep getting and posting /socket.io/?EIO=3&transport=polling&t= 【发布时间】:2020-10-16 06:48:04 【问题描述】:我在 ExpressJS 和 NodeJS 中的后端曾经运行良好。我刚刚意识到日志爆炸了磁盘(后端仍然可以运行),因为当后端打开时,它一直在尝试:
kpi.js GET /socket.io/?EIO=3&transport=polling&t=NBiaEK6
index.js GET /socket.io/?EIO=3&transport=polling&t=NBiaEK6
index.js router.get *
kpi.js POST /socket.io/?EIO=3&transport=polling&t=NBiaER6
index.js POST /socket.io/?EIO=3&transport=polling&t=NBiaER6
Error: Not Found
at /opt/funfun/app.js:99:13
at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
at /opt/funfun/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
at /opt/funfun/node_modules/express/lib/router/index.js:635:15
at next (/opt/funfun/node_modules/express/lib/router/index.js:260:14)
at /opt/funfun/routes/index.js:18:2
at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
at /opt/funfun/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/opt/funfun/node_modules/express/lib/router/index.js:174:3)
at router (/opt/funfun/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
at /opt/funfun/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
at /opt/funfun/node_modules/express/lib/router/index.js:635:15
kpi.js GET /socket.io/?EIO=3&transport=polling&t=NBiaF8A
index.js GET /socket.io/?EIO=3&transport=polling&t=NBiaF8A
index.js router.get *
kpi.js POST /socket.io/?EIO=3&transport=polling&t=NBiaFFz
index.js POST /socket.io/?EIO=3&transport=polling&t=NBiaFFz
Error: Not Found
at /opt/funfun/app.js:99:13
at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
at /opt/funfun/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
at /opt/funfun/node_modules/express/lib/router/index.js:635:15
at next (/opt/funfun/node_modules/express/lib/router/index.js:260:14)
at /opt/funfun/routes/index.js:18:2
at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
at /opt/funfun/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/opt/funfun/node_modules/express/lib/router/index.js:174:3)
at router (/opt/funfun/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/opt/funfun/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/opt/funfun/node_modules/express/lib/router/index.js:317:13)
at /opt/funfun/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/opt/funfun/node_modules/express/lib/router/index.js:335:12)
at next (/opt/funfun/node_modules/express/lib/router/index.js:275:10)
at /opt/funfun/node_modules/express/lib/router/index.js:635:15
kpi.js GET ...
和 index.js GET ...
是我打印的。这是opt/funfun/app.js
的代码:
// catch 404 and forward to error handler
app.use(function (req, res, next)
var err = new Error('Not Found'); // line 99
err.status = 404;
next(err);
);
有人知道是什么原因吗?
【问题讨论】:
【参考方案1】:如果您有一个 socket.io
客户端尝试连接到
您的后端,但您的后端没有配置 socket.io
服务器或
未正确配置为接受连接,并且您正在记录任何错误
错误处理程序。
确保正确配置了 socket.io 客户端和服务器。 请参阅socket.io docs 了解如何设置客户端和服务器。
默认情况下,当连接失败时,客户端会无限尝试重新连接,
您可以通过设置
reconnectionAttempts
选项。
例如,这将阻止客户端在 10 次尝试失败后轮询服务器
const socket = io(serverURL, reconnectionAttempts: 10 );
另外为了最小化你的日志大小,当你在production
时不要记录整个错误object
,
您可以更新错误处理程序以仅记录相关详细信息
// catch errors and forward to error handler
app.use(function (req, res, next)
...
next(err);
);
//In the error handler
app.use(function (err, req, res, next)
//log only relevant details
logger.info(err.message)
)
【讨论】:
【参考方案2】:由于您将错误传递给next
参数,它将检测为错误,如果您没有任何错误处理程序,Express 将使用其默认处理程序。
如果您将任何内容传递给 next() 函数(字符串 'route' 除外),Express 会将当前请求视为错误,并将跳过任何剩余的非错误处理路由和中间件函数。
如果你将错误传递给 next() 并且你没有在自定义错误处理程序中处理它,它将由内置错误处理程序处理;错误将通过堆栈跟踪写入客户端。堆栈跟踪不包含在生产环境中。
发件人:https://expressjs.com/en/guide/error-handling.html
可能是因为:
-
您没有错误处理程序中间件,Express 默认错误处理程序正在记录所有错误堆栈跟踪
您有错误处理程序,但它正在记录所有错误堆栈跟踪。
首先,确保您在生产环境中运行,因此错误堆栈跟踪不会发送到客户端
将环境变量 NODE_ENV 设置为生产,以在生产模式下运行应用程序。
如果您想将数据(错误)传递给下一个中间件而不被检测为错误,请将其存储在 res.locals
中,阅读更多 here。
app.use(function (req, res, next)
const err = new Error("Not Found");
err.status = 404;
res.locals.exception = err;
next();
);
// Custom Error handler
app.use(function (req, res)
let err = res.locals.exception;
// Add Minimum logging here if you want
if(err.message === "Not Found")
return res.status(404).send();
else
return res.status(500).send();
// or
return res.status(err.status).send(err.message);
);
或编写您自己的实际自定义错误处理程序中间件(确保将其放在 404 错误处理程序之后)
app.use(function (req, res, next)
const err = new Error("Not Found");
err.status = 404;
next(err);
);
// Actual Error handler Middleware, notice the first parameter is the error
app.use(function (err, req, res)
// Add Minimum logging here if you want
if(err.message === "Not Found")
return res.status(404).send();
else
return res.status(500).send();
// or
return res.status(err.status).send(err.message);
);
// -- or if you are using typescript --
// declare the error handler first
const errorHandler: ErrorRequestHandler = (err, req, res, next) =>
// Add Minimum logging here if you want
if(err.message === "Not Found")
return res.status(404).send();
else
return res.status(500).send();
// or
return res.status(err.status).send(err.message);
;
// then use it
app.use(errorHandler);
我一直在使用第一个选项 ( 我只是想通了,将其添加到示例中,找到了解决方案来自this github issueres.locals
),因为在 Typescript 上它不支持错误处理程序中间件。
【讨论】:
以上是关于继续获取和发布 /socket.io/?EIO=3&transport=polling&t=的主要内容,如果未能解决你的问题,请参考以下文章
Phaser.js 中的 Node.js 和 Socket.io 未连接 socket.io/?EIO=3&transport=polling
socket.io/?EIO=3&transport=polling&t=1345678 不充电
如何从 url 中删除 socket.io EIO 和其他参数
GET http://localhost:8080/socket.io/?EIO=3&transport=polling&t=McNiz_D 404(未找到)
反应客户端:websocket.js:83 WebSocket 连接到 'ws://localhost:3009/socket.io/?EIO=4&transport=websocket' 失