Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalid non-string/buffer chunk Node.js

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);
like image 548
Pablo Mateos Avatar asked Mar 17 '15 01:03

Pablo Mateos


1 Answers

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.

like image 117
Jasper Woudenberg Avatar answered Nov 02 '22 16:11

Jasper Woudenberg