Missing error handler on 'socket'. and typeerror: cannot read property 'on' of undefined

Missing error handler on 'socket'. and typeerror: cannot read property 'on' of undefined

我在一个 NodeJS 服务器上工作,我试图使用多个 socket.emit 但每次我尝试这样做时,我都会在 'socket' 上得到一个缺少的错误处理程序。和 TypeError:无法读取 undefined 的 属性 'on'。有什么办法可以解决这个问题并保留两个 socket.emit 吗?

部分服务器代码损坏:

listener.sockets.on('connection', function(socket){
    //send data to client
    setInterval(function(){
        socket.emit('date', {'date': new Date()});
    }, 1000);

    var size = 0;

    //recieve client data
    socket.on('client_data', function(data){
        socket.emit('returnvar', {'returnvar': data.letter});
        socket.broadcast.emit('returnvar', {'returnvar': data.letter});
        process.stdout.write(data.letter);

        var res1 = data.letter.split("",13);

        if(res1 == "h,t,t,p,s,:,/,/,y,o,u,t,u" || res1 == "h,t,t,p,s,:,/,/,w,w,w,.,y" ){

            process.stdout.write("GOOGOGOOGOGOGOGOOGOGOOGOGOGOGOOGOGO");

            var video = youtubedl(data.letter,
                // Optional arguments passed to youtube-dl.
                ['--format=18'],
                // Additional options can be given for calling `child_process.execFile()`.
                { cwd: __dirname });

            // Will be called when the download starts.
            video.on('info', function(info) {
                console.log('Download started');
                console.log('filename: ' + info._filename);
                console.log('size: ' + info.size);
                size = info.size;
            });

            video.pipe(fs.createWriteStream('video.mp4'));
        }

        var pos = 0;

        video.on('data', function data(chunk) {
            'use strict';
            pos += chunk.length;

            // `size` should not be 0 here.
            if (size) {
                var percent = (pos / size * 100).toFixed(2);
                process.stdout.cursorTo(0);
                process.stdout.clearLine(1);
                process.stdout.write(percent + '%');
                socket.emit('returnvar1', {'returnvar1': percent + '%'});
                socket.broadcast.emit('returnvar1', {'returnvar1': percent + '%'});
            }
        });

    });

here is the image of the error

好吧,经过大量时间只是把随机代码放在各处,我找到了这个解决方案:

    listener.sockets.on('connection', function(socket){
    //send data to client
    setInterval(function(){
        socket.emit('date', {'date': new Date()});
    }, 1000);

    var size = 0;

    //recieve client data
    socket.on('client_data', function(data){
        socket.emit('returnvar', {'returnvar': data.letter});
        socket.broadcast.emit('returnvar', {'returnvar': data.letter});
        process.stdout.write(data.letter);

        var res1 = data.letter.split("",13);

        if(res1 == "h,t,t,p,s,:,/,/,y,o,u,t,u" || res1 == "h,t,t,p,s,:,/,/,w,w,w,.,y" ){

            process.stdout.write("GOOGOGOOGOGOGOGOOGOGOOGOGOGOGOOGOGO");

            var video = youtubedl(data.letter,
                // Optional arguments passed to youtube-dl.
                ['--format=18'],
                // Additional options can be given for calling `child_process.execFile()`.
                { cwd: __dirname });

            // Will be called when the download starts.
            video.on('info', function(info) {
                console.log('Download started');
                console.log('filename: ' + info._filename);
                console.log('size: ' + info.size);
                size = info.size;
            });


             var pos = 0;

        video.on('data', function data(chunk) {
            'use strict';
            pos += chunk.length;

            // `size` should not be 0 here.
            if (size) {
                var percent = (pos / size * 100).toFixed(2);
                process.stdout.cursorTo(0);
                process.stdout.clearLine(1);
                process.stdout.write(percent + '%');
                socket.emit('returnvar1', {'returnvar1': percent + '%'});
                socket.broadcast.emit('returnvar1', {'returnvar1': percent + '%'});
            }
        });

            video.pipe(fs.createWriteStream('video.mp4'));
        }



    });

我认为问题是 "socket is not define"。像下面这样尝试。

listener.sockets.on('connection', function(socket){
    socket.on('client_data', function(data){ ... }); // here socket is define
    ... 
    //send data to client
    setInterval(function(){
        socket.emit('date', {'date': new Date()});
    }, 1000);
});