Created
September 10, 2014 18:37
-
-
Save alanhoff/80f986b671271d1af7c3 to your computer and use it in GitHub Desktop.
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 characters
| var net = require('net'); | |
| var HOST = '127.0.0.1'; | |
| var PORT = 8080; | |
| var client = new net.Socket(); | |
| client.connect(PORT, HOST, function() { | |
| console.log('CONNECTED TO: ' + HOST + ':' + PORT); | |
| // Write a message to the socket as soon as the client is connected, | |
| // the server will receive it as message from the client | |
| var json = {id: '17', data: 'marcos'}; | |
| setInterval(function(){ | |
| client.write(JSON.stringify(json) + '\r\n'); | |
| }, 10); | |
| }); | |
| // Add a 'data' event handler for the client socket | |
| // data is what the server sent to this socket | |
| client.on('data', function(data) { | |
| console.log('DATA: ' + data); | |
| console.log('\n'); | |
| // Close the client socket completely | |
| //client.destroy(); | |
| }); | |
| // Add a 'close' event handler for the client socket | |
| client.on('close', function() { | |
| console.log('Connection closed'); | |
| }); |
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 characters
| var net = require('net'); | |
| var buffertools = require('buffertools').extend(); | |
| // Definindo variaveis de conexão | |
| var HOST = 'localhost'; | |
| var PORT = 8080; | |
| //criando server | |
| var server = net.createServer(); | |
| //ouvindo conexão | |
| server.listen(PORT, HOST); | |
| var count = 0; | |
| var delimitter = new Buffer('\r\n'); | |
| server.on('connection', function(socket){ | |
| //logged connection | |
| count++; | |
| console.log(count); | |
| console.log('New connection recived from: '+ socket.remoteAddress +':'+ socket.remotePort); | |
| socket.write('Welcome!\n'); | |
| var buffer = new Buffer(0); | |
| socket.on('data', function(data){ | |
| //console.log(data); | |
| //data = new Buffer(data, 'utf-8'); | |
| buffer = buffer.concat(buffer, data); | |
| if(buffer.indexOf(delimitter) === -1){ | |
| //console.log('invalid'); | |
| socket.write('invalid data!\n'); | |
| return; | |
| } | |
| var json = []; | |
| var loop = function(){ | |
| if(buffer.indexOf(delimitter) === -1) | |
| return; | |
| json.push(buffer.slice(0, buffer.indexOf(delimitter))); | |
| buffer = buffer.slice(buffer.indexOf(delimitter) +1); | |
| loop(); | |
| }; | |
| loop(); | |
| json.forEach(function(jdata){ | |
| jsonParser(socket, jdata); | |
| }); | |
| /*data = JSON.parse(data); | |
| console.log('DATA RECIVED: '+data); | |
| console.log(data.id);*/ | |
| }); | |
| socket.on('close', function(data){ | |
| console.log('CONN CLOSE: '+data); | |
| }); | |
| socket.on('error', function(err){ | |
| console.log('CRASHED: '+err); | |
| }); | |
| socket.on('end', function(){ | |
| console.log('CONN END'); | |
| }); | |
| }); | |
| var jsonParser = function(socket, json){ | |
| //data = JSON.parse(json); | |
| //console.log(json.toString()); | |
| socket.write('recived by id: ' + json +'\n'); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment