2

How do I remove the newline from this code?

socket.on('data', function(data){ console.log('Data in server, sending to handle()'); worker.handle(data, socket); }); 

Worker.handle():

exports.handle = function handle(command, socket) { console.log('Data sent to handle()'); command = command.toString(); 

console.log(command);

Edit:

I am getting this output:

test data [newline] 

Edit 2:

Here is the continuing code:

if (command === 'look') { //stuff } if (command === 'login') { //stuff 
6
  • Where are you seeing the newline? Can you post some output too and specify the newline in it Commented Jul 28, 2012 at 15:48
  • 2
    See stackoverflow.com/questions/6157497/… Commented Jul 28, 2012 at 16:40
  • This has nothing to do with the console.log function. This is because of your toString function. What does that function look like? Commented Jul 28, 2012 at 17:12
  • It's not a function. See nodejs.org/api/… Commented Jul 28, 2012 at 17:16
  • 1
    Well it is a function, but not user-defined :) Commented Jul 28, 2012 at 18:40

1 Answer 1

3

This isn't a display / presentation problem. This is a problem related to data transmission protocols. Socket is a stream oriented protocol which means it isn't message based. Meanwhile you are using it like it is message based - which you can do but then you need to define a protocol for your sender and receiver to identify the start and end of each message.

Having said this and based on what you are asking I'm assuming that you've settled on using a newline (or some variant of one) as your message end marker. To make this work properly you need to actively look for that newline in the incoming data so you can recognize the end of each message as well as strip it off prior to processing.

The following code should replace your socket.on method to get the result you want.

// define your terminator for easy reference, changes var msgTerminator = '\n'; // create a place to accumulate your messages even if they come in pieces var buf; socket.on('data', function(data){ // add new data to your buffer buf += data; // see if there is one or more complete messages if (buf.indexOf(msgTerminator) >= 0) { // slice up the buffer into messages var msgs = buf.split(msgTerminator); for (var i = 0; i < msgs.length - 2; ++i) { // walk through each message in order var msg = msgs[i]; // pick off the current message console.log('Data in server, sending to handle()'); // send only the current message to your handler worker.handle(msg, socket); } buf = msgs[msgs.length - 1]; // put back any partial message into your buffer } }); 
Sign up to request clarification or add additional context in comments.

4 Comments

I got an error: if (buf.instr(msgTerminator) >= 0) { ^ TypeError: Object undefinedlook has no method 'instr' at Socket.<anonymous> (/home/otto/creativemud/server.js:24:17) at Socket.EventEmitter.emit (events.js:88:17) at TCP.onread (net.js:403:14)
Sorry, thinking in the wrong language. Replace instr with indexOf.
whiskers75, how did it work out for you? Mind marking it as solved if so?
what the heck is this even true when reading a file sync! I don`t understand why the \n is there

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.