Last active
August 29, 2015 14:06
-
-
Save alanhoff/1509272bf7baf7032546 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
var net = require('net'); | |
var buffertools = require('buffertools').extend(); | |
var delimitador = new Buffer('\r\n'); | |
var server = net.createServer(function(socket){ | |
console.log('Socket conectado.'); | |
var buffer = new Buffer(0); | |
socket.on(data, function(buff){ | |
// Acrescentamos os dados ao buffer | |
buffer = Buffer.concat([buffer, buff]); | |
// Verificamos se existe algum comando no buffer | |
if(buffer.indexOf(delimitador) === -1) | |
return; // Não achamos o delimitador | |
var comandos = []; | |
var loop = function(){ | |
// pegamos todos os comandos até não existir mais comandos | |
if(buffer.indexOf(delimitador) === -1) | |
return; | |
comandos.push(buffer.slice(0, buffer.indexOf(delimitador))); | |
// Alteramos o buffer retirando o comando | |
buffer = buffer.slice(buffer.indexOf(delimitador) + 1); | |
// reiniciamos o loop | |
loop(); | |
}; | |
// disparamos todos os comandos recebidos | |
comandos.forEach(function(command){ | |
commandParser(socket, command); | |
}); | |
}); | |
}); | |
var commandParser = function(socket, command){ | |
console.log('commando recebido'); | |
console.log(command.toString()); | |
}; | |
server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment