1

I have created my web socket server and client using this simple tutorial here : http://cjihrig.com/blog/creating-your-own-node-js-websocket-echo-server/

But it seems like it recognises only UTF-8 characters.
I want to send JSON message in the text box:

var jsonString = JSON.stringify({"fname":"John","lname":"Smith"})

and in ws_server.js file, I have written

connection.on('message', function(message) {  
var jObject = JSON.parse(message); 
jObject.lname = "Jobs";  
}..  

But I am getting error for JSON.parse method.

Please let me know what is the problem.
Or do I have to write any new server implementation for JSON parsing or JSON messages?

This is my client code: http://cjihrig.com/blog/creating-your-own-websocket-echo-client/
but value of "text" is modified to be as follows:
jsonmsg = {fname:"John",lname:"Smith"}
jsonString = JSON.stringify(jsonmsg);
So socket.send(jsonString);

Thanks
Sneha

5
  • what does the message param look like? console.log(message) and see what it looks like. Commented Jun 15, 2012 at 3:51
  • message param prints "Object". so it is an object and when I do console.log(message.type), it is giving me UTF-8!! Commented Jun 15, 2012 at 3:56
  • what happens if you don't do JSON.parse(message) and just use message.lname? Commented Jun 15, 2012 at 3:58
  • it is not working.. thorows error Commented Jun 15, 2012 at 4:06
  • can you post your client code? how are you sending the message over? Commented Jun 15, 2012 at 4:12

2 Answers 2

1

Have you tried:

socket.json.send( { fname : 'John', lname : 'Smith'} );
Sign up to request clarification or add additional context in comments.

Comments

1
var jsonString = {"fname":"John","lname":"Smith"}

does not create a string; it creates an object. To make it a string:

var jsonString = JSON.stringify({"fname":"John","lname":"Smith"})

1 Comment

yes.. I am using the same .. But I have problem with parse method at server side.

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.