Best way to treat string in nodes

Asked

Viewed 183 times

1

I am working on an application using a socket for an application on my server. I’m having trouble handling the calls generated by the net module.

My application is in real time, that is, every 200ms I send a request for the socket so that it can return some information, but sometimes it is necessary to make other calls and so my life gets complicated to handle these response strings.

What I am doing now is treating the string with the "split" javascript function, but I wonder if there is a more intuitive way to do this.

Follows the code:

mvcp.on("data", function(chunk) {
//console.log(chunk)
split_tempos = "";
index = "";
total_index = "";
allchunk = chunk;
str = allchunk.toString();

//pra verificar se é um list ou um usta
split_verify = str.split('\n');
if(!parseInt(split_verify[1])){
    if (!(str.indexOf("202 OK") > -1) && !(str.indexOf("201 OK") > -1)) {
        split_todos = str.split(" ");
        split_nome = str.split("\"");
        split_dados = split_nome[2];
        split_playlist_info = split_nome[4];
        if (typeof split_dados != 'undefined' && split_playlist_info != '' && typeof split_playlist_info != 'undefined') 
        {

            split_playlist_info = split_playlist_info.split(" ");
            index = split_playlist_info[7];
            if(typeof index != 'undefined'){
                if (typeof split_dados != 'undefined'){
                    index = index.split('\r');
                    index = index[0];
                    index_global = index;
                }
            }               
            total_pl = split_playlist_info[6];
        }
        if (typeof split_dados != 'undefined') {
            split_tempos = split_dados.split(" ");

        }
        var JSONresponse = {};
        JSONresponse.data = [];
        var nome = split_nome[1];
        var tamanho;
        if(typeof nome != 'undefined'){
            nome = nome.split("/");
            tamanho = nome.length - 1;
        }else
        {
            nome = "";
        }
        console.log(nome);
        JSONresponse.data.push({
            status: split_todos[1],
            filename: split_nome[1],
            nome:nome[tamanho],
            position: split_tempos[1],
            fps:split_tempos[3],
            inpoint:split_tempos[4],
            outpoint:split_tempos[5],
            length:split_tempos[6],
            b_filename: split_todos[3]
        });
        str = JSON.stringify(JSONresponse);
        io.sockets.emit("incomingMessage", str);
    }

}else
{
    list = str.split('\n');

    var dados = [];
    for(i = 2; i < (list.length -2); i++)
    {
        var dados_completos = "";
        var dados_movie = "";
        var pl_item = list[i].split("\"");

        var index = pl_item[0];
        var nome = pl_item[1];
        dados_completos = pl_item[2];
        if(typeof dados_completos != 'undefined'){
            dados_movie = dados_completos.split(' ');
            inpoint = dados_movie[3];
            outpoint = dados_movie[4];
            real_length = dados_movie[5];
            calculated_length = dados_movie[6];
        }

        var data = {index: index,index_current: index_global, nome: nome, inpoint: inpoint, outpoint: outpoint, real_length: real_length, calculated_length: calculated_length};

        dados.push(data);

    }

    io.sockets.emit("list", dados);


}

});

  • 1

    @Sergio edited with the code.

  • I don’t understand the question.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.