-
-
Save lionsole/7e7f05b5d892cad93d2aefd0c088f188 to your computer and use it in GitHub Desktop.
Revisions
-
branneman created this gist
Nov 8, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ var net = require('net'); // // Client // function openSocket() { var socket = net.connect(3e3); socket.setKeepAlive(true); socket.on('connect', onConnect.bind({}, socket)); socket.on('error', onError.bind({}, socket)); } var interval; function onConnect(socket) { console.log('Socket is open!'); interval = setInterval(function() { var msg = parseInt(+new Date) + ''; socket.write(msg, function() { console.log('Sent:', msg); }); }, 500); } function onError(socket) { console.log('Socket error!'); // Kill socket clearInterval(interval); socket.destroy(); socket.unref(); // Re-open socket setTimeout(openSocket, 1e3); } openSocket(); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ var net = require('net'); // // Server // var server = new net.Server(); server.on('connection', function(socket) { console.log('Socket is open!'); socket.setEncoding('utf8'); socket.on('data', function(data) { console.log('Received:', data); }); socket.on('error', function() { console.log('Socket error!'); }); }); server.listen(3e3, function() { console.log('Server is listening!'); });