lib/layers/TCP.js
function jsonStream(target, socket) {

		// first set the channel encodeing to utf8
		socket.setEncoding('utf8');

		// data chunks may not contain a complete json string, thats why we store the data
		var jsonBuffer = '';

		// emits when new data from TCP connection is received
		socket.on('data', function (chunk) {
			// first add the chunk to the json buffer
			jsonBuffer += chunk;

			var start = 0, i;
			// next find the next line end char
			while ((i = jsonBuffer.indexOf('\n', start)) >= 0) {

				// if one was found copy it out of the jsonBuffer string
				var json = jsonBuffer.slice(start, i);

				// now that  we have a complete json string
				// emit message with a parsed json object
				target.emit('message', JSON.parse(json));

				// set the starting point of next line end search
				start = i + 1;
			}

			// when no more complete json objects exist in the jsonBuffer,
			// we will slice the jsonBuffer so it only contains the uncomplete json string
			jsonBuffer = jsonBuffer.slice(start);
		});
	}
function jsonStream(target, socket) {

		// first set the channel encodeing to utf8
		socket.setEncoding('utf8');

		// data chunks may not contain a complete json string, thats why we store the data
		var jsonBuffer = '';

		// emits when new data from TCP connection is received
		socket.on('data', function (chunk) {
			// first add the chunk to the json buffer
			jsonBuffer += chunk;

			var start = 0, i;
			// next find the next line end char
			while ((i = jsonBuffer.indexOf('\n', start)) >= 0) {

				// if one was found copy it out of the jsonBuffer string
				var json = jsonBuffer.slice(start, i);

				// now that  we have a complete json string
				// emit message with a parsed json object
				target.emit('message', JSON.parse(json));

				// set the starting point of next line end search
				start = i + 1;
			}

			// when no more complete json objects exist in the jsonBuffer,
			// we will slice the jsonBuffer so it only contains the uncomplete json string
			jsonBuffer = jsonBuffer.slice(start);
		});
	}
function jsonStream ( target , socket ) { // first set the channel encodeing to utf8 socket . setEncoding ( 'utf8' ) ; // data chunks may not contain a complete json string, thats why we store the data var jsonBuffer = '' ; // emits when new data from TCP connection is received socket . on ( 'data' , function ( chunk ) { // first add the chunk to the json buffer jsonBuffer += chunk ; var start = 0 , i ; // next find the next line end char while ( ( i = jsonBuffer . indexOf ( '\n' , start ) ) >= 0 ) { // if one was found copy it out of the jsonBuffer string var json = jsonBuffer . slice ( start , i ) ; // now that  we have a complete json string // emit message with a parsed json object target . emit ( 'message' , JSON . parse ( json ) ) ; // set the starting point of next line end search start = i + 1 ; } // when no more complete json objects exist in the jsonBuffer, // we will slice the jsonBuffer so it only contains the uncomplete json string jsonBuffer = jsonBuffer . slice ( start ) ; } ) ; }
Since data from a socket comes in buffer streams in an unknown length this helper store the data until a complete json object has been received when a json object is received it will emit the message event on target. JSON strings will be isolated by a newline sign.
Since data from a socket comes in buffer streams in an unknown length this helper store the data until a complete json object has been received when a json object is received it will emit the message event on target . JSON strings will be isolated by a newline sign .