Socket.IO允许中间件函数传递错误。
var io = require('socket.io')();
io.use(function(socket, next){
if (socket.request.headers.cookie) return next();
next(new Error('Authentication error'));
});
客户端可以通过监听默认的“error”事件来捕获这些错误。
clientIO.on('error', function(err) {
console.log(err);
}
是否有办法使Socket.IO中间件发出自定义事件名称而不是“error”(例如,“authentication_error”)?
socket.io
(1.4.8),对我来说似乎完全正常。这里是一个代码片段,是我测试过的代码。我相信你需要使用err.data
,而不是像err.type
或其他随机属性一样。 - robertklep