簡體   English   中英

Node.js-socket.io-對io.sockets的引用突然未定義

[英]Node.js - socket.io - reference to io.sockets suddenly undefined

我有一個運行簡單的socket.io服務的node.js服務器

當我引用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);
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM