frenchtoaster10 frenchtoaster10 - 4 months ago 35
Javascript Question

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

I'm working on a NodeJS server and I'm trying to use more than one socket.emit but every time I try to do this I get a Missing error handler on 'socket'. and TypeError: cannot read property 'on' of

undefined
. Is there any way to fix this and keep both of the socket.emits?

Some of the code for the server that is broken:

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

ok after lots of time just putting random code everywhere i came to this solution:

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



});

Answer

I think that problem is "socket is not define". Try like below.

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