{"id":6694,"date":"2014-04-22T00:21:03","date_gmt":"2014-04-22T00:21:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/22\/syntaxerror-unexpected-end-of-input-nodejs-tcp-server-collection-of-common-programming-errors\/"},"modified":"2014-04-22T00:21:03","modified_gmt":"2014-04-22T00:21:03","slug":"syntaxerror-unexpected-end-of-input-nodejs-tcp-server-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/22\/syntaxerror-unexpected-end-of-input-nodejs-tcp-server-collection-of-common-programming-errors\/","title":{"rendered":"SyntaxError: Unexpected end of input (nodejs tcp server)-Collection of common programming errors"},"content":{"rendered":"<p>You could wait for emitting <strong>end<\/strong> event, and then parse it.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>\/\/ Handle incoming messages from clients.\ndata = '';\nsocket.on('data', function (chunk) {\n    data += chunk;\n});\n\n\/\/ Handle incoming messages from clients.\nsocket.on('end', function () {\n    var obj = JSON.parse(data);\n\n    if(obj.type == \"register\") {\n        _clientObj[obj.msg] = {\n            client: 'Client',\n            socketID: socket.name\n        }\n\n    console.log(\"Register...\");   \n\n    } else if(obj.type == \"message\") {\n        console.log(socket.name + \" --&gt; message: \" + obj.msg);  \n    }\n});\n<\/code><\/pre>\n<p><strong>Documentation<\/strong>: http:\/\/nodejs.org\/api\/all.html#all_event_end_1<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You could wait for emitting end event, and then parse it. Example: \/\/ Handle incoming messages from clients. data = &#8221;; socket.on(&#8216;data&#8217;, function (chunk) { data += chunk; }); \/\/ Handle incoming messages from clients. socket.on(&#8216;end&#8217;, function () { var obj = JSON.parse(data); if(obj.type == &#8220;register&#8221;) { _clientObj[obj.msg] = { client: &#8216;Client&#8217;, socketID: socket.name } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6694","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6694"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6694\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}