Created
July 16, 2010 13:12
-
-
Save 3rd-Eden/478345 to your computer and use it in GitHub Desktop.
TCP framing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) |
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.
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
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).