JSON parsing in web socket server

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

Answers


Have you tried:

socket.json.send( { fname : 'John', lname : 'Smith'} );

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"})

Need Your Help

UISearchDisplayController Results Table Overlapping UISearchBar

objective-c ios7 autolayout uisearchdisplaycontroller

I'm having a strange issue with my UISearchDisplayController. When the search display activates the frame of the background fading view and the tableview are incorrectly overlapping the UISearchBar...

How to install node.js as windows service?

node.js windows-services

I have downloaded node.js executable. How can I run that executable as windows service?