Node.js-socket.io-对io.sockets的引用突然未定义
Node.js - socket.io - reference to io.sockets suddenly undefined
我有一个node.js服务器运行一个简单的socket.io服务
当我引用this.io.sockets
来建立一个"连接"处理程序函数时,它工作得很好。但如果我后来引用this.io.sockets
,我会得到一个错误
TypeError: Cannot read property 'sockets' of undefined
我对node.js还很陌生,所以不确定我在这里做错了什么。
代码:
var app = require('http').createServer();
var port = 8080;
app.listen(port);
function Serv() {
this.io = require('socket.io')(app)
this.addHandlers()
}
Serv.prototype.addHandlers = function () {
this.io.sockets.on("connection", function (socket) {
console.log('new connection');
socket.on('disconnect', function () {
console.log('disconnection');
if (this.io.sockets.adapter.rooms[phn] != null) { //Causes undefined error..
//do something
}
});
socket.on(SOCKETEVENTMESSAGE, function (data) {
if (this.io.sockets.adapter.rooms[phn] != null) { //Causes undefined error..
//do something
}
});
});
};
// Start the server
var serv = new Serv();
console.log('socket.io listening on ' + port);
正如您所看到的,问题是显而易见的
this.io.sockets.on("connection", function (socket) {
每当发生连接时都会调用监听器,因此作用域(this
)将不同。
你应该做的是,至少有三种方法,但我建议使用一种
将作用域保存到一个变量中,并由类似的侦听器关闭
Serv.prototype.addHandlers = function () {
var _this = this;
this.io.sockets.on("connection", function (socket) {
console.log('new connection');
socket.on('disconnect', function () {
console.log('disconnection');
if (_this.io.sockets.adapter.rooms[phn] != null) {
//do something
}
});
问题是,每当您将函数声明为套接字事件的回调时,该函数都会有自己的作用域/上下文。这意味着您将丢失this
的值。默认情况下,在这种情况下,this
实际上指的是在.中运行回调函数的任何上下文套接字
JavaScript附带了一种内置的方式来确保使用您想要的上下文:bind
Serv.prototype.addHandlers = function () {
this.io.sockets.on("connection", function (socket) {
console.log('new connection');
socket.on('disconnect', function () {
console.log('disconnection');
if (this.io.sockets.adapter.rooms[phn] != null) { 'this' has correct value
//do something
}
}.bind(this));
socket.on(SOCKETEVENTMESSAGE, function (data) {
if (this.io.sockets.adapter.rooms[phn] != null) { 'this' has correct value
//do something
}
}.bind(this));
}.bind(this));
};
为了避免自己陷入回调地狱,您可以单独声明函数,如下所示:
Serv.prototype.addHandlers = function () {
var onDisconnect = function () {
console.log('disconnection');
if (this.io.sockets.adapter.rooms[phn] != null) {
//do something
}
}.bind(this);
var handleEvent = function (data) {
if (this.io.sockets.adapter.rooms[phn] != null) {
//do something
}
}.bind(this);
function onConnect (socket) {
console.log('new connection');
socket.on('disconnect', onDisconnect);
socket.on(SOCKETEVENTMESSAGE, onHandleEvent);
}; // 'this' isn't required, so no binding necessary
this.io.sockets.on("connection", onConnect);
};
相关文章:
- 引用对象中的通用值
- 如何在JavaScript中将字符串转换为函数引用
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- jQuery UI自动完成突然停止工作
- 在动态创建的元素上获取对特定选择器的引用
- IIFE中的函数引用不可用
- 如何通过引用var Using DataTables来进行分页或排序
- 如何在创建键时引用来自同一对象的键
- Datatables:通过DOM数据源中的名称引用列
- 自引用回调
- 引用类变量中的原型方法
- 无法获取属性'selectedIndex'的未定义引用或null引用
- Vanilla JS通过引用移除数组元素
- 通过引用传递JavaScript对象
- 无法获取属性'Id'使用Knockout.js的未定义或空引用API
- "“;变量未引用正确的对象
- 获取对使用Tampermonkey使用javascript创建的元素的引用
- 引用vue.js中v-for中的上一个值
- 在gump和nodejs中使用Typescript时,未定义对require和exports的引用
- Node.js-socket.io-对io.sockets的引用突然未定义