Reputation: 17149
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.
Upvotes: 1
Views: 1917
Reputation: 17149
In case anyone is wondering. Solution is very simple. I just approached this the wrong way:
"use strict";
const net = require('net');
const socket = net.createConnection(6555, '198.178.123.109', ()=> {
process.stdin.pipe(socket);
});
let line = '';
socket.on('data', chunk => {
let str = chunk.toString();
for (let i = 0, len = str.length; i < len; i++) {
let chr = str[i];
line += chr;
process.stdout.write(chr);
if (/[\n\r]$/.test(chr)) {
process.stdout.write(line);
line = '';
}
}
});
Upvotes: 3