javascript 无效的非字符串/缓冲区块 Node.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29089689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
invalid non-string/buffer chunk Node.js
提问by Pablo Mateos
I'm studying Node.js in college, and it's my first time learning this kind of programming language. I've got some errors in my attempt to build a chat server. When I try to connect one client to the server, the connection closes immediately and I get the error Invalid non-string/buffer chunk
. I uploaded a screenshotfor you to check out what is wrong, because I've been thinking about it for a while and I can't find any solution.
我在大学学习 Node.js,这是我第一次学习这种编程语言。我在尝试构建聊天服务器时遇到了一些错误。当我尝试将一个客户端连接到服务器时,连接立即关闭并出现错误Invalid non-string/buffer chunk
。我上传了截图给大家看看是哪里出了问题,因为想了好久也没有找到解决办法。
My Javascript code:
我的Javascript代码:
var net = require('net');
var s = require
var sockets = [];
var nombres = [];
var nombresUsados = [];
console.log("Se ha iniciado el sevidor");
var server = net.createServer(function(socket) {
socket.push(socket);
nombres.push("Cliente:" + sockets.indexOf(socket));
nombresUsados.push("Cliente:" + socket.indexOf(socket));
console.log("Cliente aceptado, nick:" + nombres[sockets.indexOf(socket)]);
socket.write("Bienvenido" + nombres[sockets.indexOf(socket)] + "\n");
socket.on('data', function(d) {
var entrada = d.toString();
var UsuarioUnico = entrada.match(/^msg/);
var cambiarNick = entrada.match(/^nick/);
var quit = entrada.match(/^quit/);
if (cambiarNick == "nick") {
var repetido = 0;
var nombresSinNick = entrada.replace(cambiarNick, '');
for (var i = nombres.length - 1; i <= 0; i--) {
if (nombresSinNick.substring(0, nombres[i].toString().length) == nombres[i].toString()) {
socket.write("KO, escoja otro nombre\n")
repetido = 1;
}
};
if (repetido == 0) {
nombres[sockets.indexOf(socket)] == nombresSinNick.trim();
process.on('uncaughtException', function(err) {
socket.write("KO\n");
});
socket.write("OK. " + nombres[sockets.indexOf(socket)] + "\n");
console.log(nombresUsados[sockets.indexOf(socket)]) + "su nombre ha sido cambiado por:" + nombres[sockets.indexOf(socket)];
nombresUsados[sockets.indexOf(socket)] = nombresSinNick.trim();
}
} else if (UsuarioUnico = "msg") {
var nombresSinMsg = entrada.replace(UsuarioUnico, '');
var encontrado = 0;
for (var i = nombres.length - 1; i <= 0; i--) {
if (nombresSinMsg.substring(0, nombres[i].toString().length) == nombres[i].toString()) {
var mensaje = nombresSinMsg.replace(nombres[i], '');
}
};
socket.on('end', function() { // CALLBACK: desconexión de cliente
if (quit == 'quit') {
var i = nombres[sockets.indexOf(socket)];
sockets.splice(i, 1);
console.log("Ha salido el usuario:" + nombres[sockets.indexOf(socket)]);
}
});
}
});
});
server.listen(9000);
采纳答案by Jasper Woudenberg
I think the problem is the line socket.push(socket)
. Probably you mean sockets.push(socket)
. What you're doing now, is attempting to push the socket instance into the socket stream which fails because, as the error says, it's not a string or buffer.
我认为问题出在线路上socket.push(socket)
。可能你的意思是sockets.push(socket)
。您现在正在做的是尝试将套接字实例推送到失败的套接字流中,因为正如错误所说,它不是字符串或缓冲区。