Skip to content

Instantly share code, notes, and snippets.

@Viswanathantv
Created July 20, 2014 09:19
Show Gist options
  • Save Viswanathantv/029962d4ea3022453499 to your computer and use it in GitHub Desktop.
Save Viswanathantv/029962d4ea3022453499 to your computer and use it in GitHub Desktop.
implementation in node
var msgpack = require('msgpack-js');
var unirest = require('unirest');
var http = require('http');
var net = require('net');
var clients = {};
var Buffer = require('buffer').Buffer;
var HOST = '127.0.0.1';
var PORT = 6969;
net.createServer().on('connection', function(sock) {
clients[0]=sock;
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
sock.on('data', function(data) {
var msgdata = msgpack.encode(data);
var msglength = data.length;
var buffer = new buffer(10);
buffer = [0xd50000]+msglength;
console.log('DATA in string' + sock.remoteAddress + ': ' + data);
console.log(msgdata);
console.log(clients[1]);
sock.write(buffer);
sock.write(data);
unirest.post('http://localhost:3000/devices')
.headers({ 'Accept': 'application/json', 'Content-type' : 'application/json' })
.send({"hid":"switch1","name":"1234"} )
.end(function (response) {
console.log(response.body);
});
console.log("sent to the server");
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});
}).listen(PORT, HOST);
var requestListener = function (req, res) {
res.writeHead(200);
res.end('You have posted!\n');
sock=clients[0];
sock.write("data from http server");
}
var server = http.createServer(requestListener);
server.listen(8080);
console.log("running on 8080");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment