我正在研究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'));
}
});
答案 0 :(得分:0)
我认为问题是"套接字未定义"。尝试如下。
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);
});