Skip to content

Instantly share code, notes, and snippets.

@yunderboy
Created August 23, 2017 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yunderboy/e7876dc0330c123cd83be99d3222d8c2 to your computer and use it in GitHub Desktop.
Save yunderboy/e7876dc0330c123cd83be99d3222d8c2 to your computer and use it in GitHub Desktop.
Socket server til vægt test
var net = require('net');
var HOST = '0.0.0.0';
var PORT = 8080;
// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() becomes the event handler for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection
net.createServer(function(sock) {
// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
// Add a 'data' event handler to this instance of socket
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ': ' + data);
// Write the data back to the socket, the client will receive it as data from the server
sock.write('You said "' + data + '"');
});
// 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);
console.log('Server listening on ' + HOST +':'+ PORT);
/*var net = require('net');
var server = net.createServer(function(sock) {
console.log('client connected');
sock.on('end', function() {
console.log('client disconnected');
});
setInterval(function () {
sock.write('100\r\n');
}, 500);
sock.pipe(sock);
});
server.listen(8080, function() {
console.log('server is listening');
});*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment