我正在使用NodeJS服务器,并且尝试使用多个socket.emit,但是每次尝试执行此操作时,都会在'socket'上看到Missing错误处理程序。和TypeError:无法读取undefined的属性“ on”。有什么办法可以解决此问题,并保持两个socket.emits?

服务器已损坏的一些代码:

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'));
        }



    });

最佳答案

我认为问题是“套接字没有定义”。尝试如下。

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);
});

关于javascript - “套接字”上缺少错误处理程序。和typeerror:无法读取未定义的属性“on”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38705404/

10-08 22:38