Working with telnet data


#1

Hello!

This not yet an electron question, but just a nodejs. I decided to write a mud client using electron js. Mud is a text based RPG game that uses telnet protocol. Here is my code so far (i just started with nodejs)

"use strict";

const net = require('net');
const TelnetInput = require('telnet-stream').TelnetInput;
const TelnetOutput = require('telnet-stream').TelnetOutput;

let socket = net.createConnection(6555, '198.178.123.109', function() {
    let input = new TelnetInput();
    let output = new TelnetOutput();

    //socket.pipe(input).pipe(process.stdout);
    process.stdin.pipe(output).pipe(socket);

    socket.pipe(input).on('data', function(chunk) {
        chunk = chunk.toString() + 'end';

        let split = chunk.match(/[^\r\n]+/g);

        split.forEach(function(line) {
            if (line.match(/end/) == null) {
                line = line + ">--\n\r";
            } else {
                line = line.replace('end', '');
            }

            process.stdout.write(line);
        });
    });
    
    input.on('will', function(option) {
        if (option == 201) {
            output.writeDo(201);

            var support = new Buffer(1);
            support.write('Core.Supports.Set [ "Room 1", "Char 1", "Comm 1", "Group 1" ]', 0);
            output.writeSub(201, support);
        }
    });

    input.on('sub', function(option, buffer) {
            // console.log(option);
            // console.log(buffer.toString());
    });
});

At the moment my problem is, that socket.pipe(input).on('data'... does not trigger on each line, but it returns multiple lines. And sometimes the these chunks do not break on new line break.

    socket.pipe(input).on('data', function(chunk) {
        console.log(chunk.toString());
        console.log("testing\n\r");
    });

The code above would give the result below. data event is triggered 2 times.

#############################################################################
##[        --- Welcome to Aardwolf MUD ---        ]##########################
##[                                               ]##########################
##[         Players Currently Online: 3           ]##########################
##[                                               ]##########################
##[     Game Last Rebooted on 06 Jun 18:21:36     ]##########################
#############################################################################
########                                                              #######
########                                                              #######
########                                                              #######
########       _____ _____ ____ _____    ____   ___  ____ _____       #######
########      |_   _| ____/ ___|_   _|  |  _ \ / _ \|  _ \_   _|      #######
########        | | |  _| \___ \ | |    | |_) | | | | |_) || |        #######
########        | | | |___ ___) || |    |  __/| |_| |  _ < | |        #######
########        |_| |_____|____/ |_|    |_|    \___/|_| \_\|_|        #######
########                                                              #######
########                                                              #######
########                                                              #######
##
testing

######                                                              #######
########                                                              #######
#############################################################################
-----------------------------------------------------------------------------
    Enter your character name or type 'NEW' to create a new character
-----------------------------------------------------------------------------
What be thy name, adventurer? 
testing

So my question is, how could i trigger an event on each/row line? I can split the chunks, but how do i concat different chunks in case the chunk does not end with a line break?

Or perhaps my approach is 100% wrong. These are pretty much my first nodejs lines.


#2

OK. I think the question was too messy.

socket ‘data’ event is a stream buffer. Sometimes the entire data is sent in chunks. So my question is, how will i know, if the chunk is entire stream message or is it just a partial?

The example above is sent in 2 chunks. So i should need to detect, if the first chunk is a partial and i have to wait for the next one. And when the next one comes, i should detect, that this is the last chunk.