Skip to content

Instantly share code, notes, and snippets.

@ChazAttack73
Created January 3, 2016 05:32
Show Gist options
  • Select an option

  • Save ChazAttack73/2f20ac17cc8fd7f25753 to your computer and use it in GitHub Desktop.

Select an option

Save ChazAttack73/2f20ac17cc8fd7f25753 to your computer and use it in GitHub Desktop.
Server Example for HTTP-Socket-Server
var net = require('net');
// ask NET module for new Server also setting what happens when client connects
var server = net.createServer(whenSomeoneConnects);
// server's connect event
function whenSomeoneConnects(socketReq) {
console.log('SOMEONE CONNECTED TO MY SERVER');
socketReq.on('data', function(buffer){
console.log('socket DATA event has fired off');
// change the buffer into a string so you can work with it!
console.log(buffer.toString().split('\n'));
// process what the user wants
// ...
// ...
//
// send stuff back to the user, EXAMPLE IS NOT REAL HEADERS :>
socketReq.write('Api-key: klsfj;laksfjlaskjf;');
socketReq.write('Content-Specific-*: klsfj;laksfjlaskjf;dklsafjklsadf');
socketReq.write('\n\n');
socketReq.write(''); // remember that the body and headers need a space between
socket.Req.write('THIS IS TEXT IS THE BODY');
socketReq.end(); // invoke the end event on the socket to tell the client you are done.
});
// might not need!
//
// socketReq.on('end', function(){
// console.log('socket end event.');
// });
// socketReq.end();
}
server.listen({ port: 8082 }, function() {
address = server.address();
console.log("opened server on %j", address);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment