1

I am being new to node.js , coming from js background i have quite harder time to understand concept behind back end stuff, currently i am trying to create simple console chat. Code for server :

var net=require("net");
var sockets=[];
net.createServer(function(socket){
    sockets.push(socket);

    socket.on("data",function(data){
        posliSpravu(socket,JSON.parse(data));
    });

    socket.on("close",function(){
        console.log(socket + " has disconnected");
        sockets.splice(sockets.indexOf(socket),1);
    })

}).listen(8888);

function posliSpravu(from,message){
    var msg=JSON.stringify("Niekto povedal : " + message)+'\n';
    sockets.forEach(function(incoming_socket){
        if(incoming_socket!=from){
            incoming_socket.write(msg);
        }
    })
}

code for client

var net=require("net");
var client=new net.Socket();
client.connect(8888,function(){
    console.log("Connected");
});
process.stdin.resume();
process.stdin.on("data",function(data){
    posli(data);
})
client.on("data",function(data){
    console.log("Recieved data === " + JSON.parse(data));
})



function posli(msg){
    client.write(JSON.stringify(msg)+'\n');
}

When i am trying to send some message , it always outputs "Message recieved === [Object][Object]"

I tried to replace JSON.parse with JSON.strngify , but output was only some numer , propably test represented in ascii.

How can i fix it so it would display text i send<

6
  • 1
    "Message recieved === [Object][Object]" means you're trying to print objects as strings or that you attempted to send an object over the wire without stringifying it first. Try just printing the objects by themselves (not preceded by "Message received") and see what happens. Commented Jan 28, 2016 at 23:11
  • i tried it and the output is still [object][object] Commented Jan 28, 2016 at 23:22
  • if no answer is given untill i wake up, i'll tell you a little about buffers in node, how they work and whats causing the trouble you ran into. Commented Jan 28, 2016 at 23:28
  • 1
    This is probably unrelated to your current issue, but you should know that you should not assume that each 'data' event will contain a complete JSON message. 'data' events can contain any number of bytes, so you could get half of a message in one event and the other half in the next. There are many ways to solve this (some more efficient than others). An easy solution is to use something like the built-in readline module with newline-terminated JSON strings/payloads. Commented Jan 28, 2016 at 23:30
  • @GottZ that would be great : ) Commented Jan 29, 2016 at 13:32

2 Answers 2

1

socket.setEncoding('utf8'); and process.stdin.setEncoding('utf8'); is your friend. you just need to add it once before the line you wait for data and everything should work as you thought about it initially.

Sign up to request clarification or add additional context in comments.

3 Comments

great! thanks , i now recieve message but with \r\n at the end , is there any way how to prevent this or is the only way to regExp it?
\r\n is there for a reason. your process.stdin is reading line buffered. this is causing the stdin to fire data only as soon as you press return. return is btw equal to \r\n
you could simply remove it by using something like regex: msg = msg.replace(/\r\n$/, ""); this will strip away \r\n at the end of a string (if it actually is found) if its not found the output is the same as the input.
0

You are calling JSON.stringify on a string. Make sure you use a valid object. Also better to send actual JSON over the wire:

//server
var msg=JSON.stringify({ name: 'Niekto povedal', content: message });

// client
var message = JSON.parse(data);
console.log("Recieved data from :" + message.name);
console.log("Recieved data === " + JSON.stringify(message.content));

1 Comment

Recieved data from :Niekto povedal Recieved data === {"type":"Buffer","data":[97,104,111,106,13,10]} This is what i get , still ASCII codes for characters .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.