7

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 screenshot for you to check out what is wrong, because I've been thinking about it for a while and I can't find any solution.

My Javascript code:

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);
1
  • 1
    Screenshot link is dead. @Pablo please remove or update.
    – Max
    Mar 23, 2019 at 0:46

1 Answer 1

5

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.

1
  • Hi @PabloMateos if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. Mar 17, 2015 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.