Skip to content

Instantly share code, notes, and snippets.

@3rd-Eden
Created July 16, 2010 13:12
Show Gist options
  • Save 3rd-Eden/478345 to your computer and use it in GitHub Desktop.
Save 3rd-Eden/478345 to your computer and use it in GitHub Desktop.
TCP framing
var net = require('net'),
sys = require('sys');
var stream = new net.Stream();
stream.addListener( 'connect', function(){
this.setTimeout( 0 );
this.setNoDelay( true );
});
stream.addListener( 'data', function( data ){
sys.puts("I have been called");
});
stream.addListener( 'end', function(){
sys.puts( "end" )
});
stream.addListener( 'error', function( error ){
sys.error( error );
});
stream.addListener( 'close', function(){
sys.puts( 'closing' );
});
stream.connect( 11211, "10.211.55.6" );
var count = 1,
tictok = function(){
if( count <= 10 ){
if( stream.readyState == 'open' && !( stream._writeQueue && stream._writeQueue.length )){
stream.write( "get testkey \r\n" );
count++;
}
process.nextTick( tictok );
} else {
stream.end();
}
}
process.nextTick( tictok )
@3rd-Eden
Copy link
Author

$ node test.js
I have been called
I have been called
I have been called
end
closing


The data listener is only called 3 times, wtf ?

@3rd-Eden
Copy link
Author

I have been called
END
END

I have been called
END
END
END

I have been called
END
END
END
END
END

end
closing

When I write out the data, so it seems to combined by node.

@kurokikaze
Copy link

Two messages that sent out too rapidly can be interpreted as one. I've seen it when developing my work queue server on Node.js. You should just separate messages by EOL (or some other termination symbol).

@kurokikaze
Copy link

Here is my gist: http://gist.github.com/390566#file_server.js
I'm sending valid JSON so I can simply look for "}{" parts and cut messages right there.

@3rd-Eden
Copy link
Author

Thanks,

I wish my responses where that easy :P, Kriszyp also shared a possible solution for this issue: http://github.com/kriszyp/multi-node/blob/master/lib/multi-node.js#L99-144

Will check out both techniques and see how it could fit :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment