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 )
@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