-
-
Save ollym/eb46af138d604dabb5c7 to your computer and use it in GitHub Desktop.
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
// We want a tcp socket | |
var socket = require('net').socket.tcp; | |
// Instantiate a new socket listening on port 8010 at 'localhost' | |
var sock = new socket(8010, 'localhost') | |
// Listen out for incoming connections | |
socket.listen(function(stream) { | |
// Log the connection | |
console.log('connection from address: ' + stream.endpoint()); | |
// Set no delay | |
stream.delay = false; | |
stream.timeout = 0; | |
stream.encoding = 'utf8'; | |
// Means data is buffered when written and flush() needs to be called to send the data | |
stream.autoflush = false; | |
// Listen for data | |
stream.events.bind('data', function(data) { | |
// Log the incoming message | |
console.log('message received from address: ' + stream.endpoint() + ' message: ' + data); | |
// Write separate messages | |
stream.write('thank you!'); | |
stream.write('very much!'); | |
// Flush the message | |
stream.flush(); | |
// stream.close(true) would close the stream and flush! | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment