Skip to content

Instantly share code, notes, and snippets.

@toriaezunama
Last active April 23, 2018 00:50
Show Gist options
  • Save toriaezunama/d19d237d32147a2b1b0317b9d3229c9a to your computer and use it in GitHub Desktop.
Save toriaezunama/d19d237d32147a2b1b0317b9d3229c9a to your computer and use it in GitHub Desktop.
node js example to demonstrate socket lifecycle events
const net = require('net');
const { spawn } = require('child_process');
const HOST = '127.0.0.1';
const PORT = 6969;
// Server /////
const _logServer = (p) => (s) => console.log(`[Server:${p}]`, s);
const logServer = _logServer('main');
const server = net.createServer();
server.listen(PORT, HOST, () => {
const info = server.address();
logServer(`Server listening on ${info.address}:${info.port}`);
});
const setupSocket = (sock, logServer, shouldClose=false) => {
sock.on('data', data => {
logServer(`DATA ${sock.remoteAddress}:${data}`);
// Echo data back to the socket
sock.write(`You said "${data}"`);
if(shouldClose) {
logServer('Ending socket')
sock.end()
}
});
sock.on('end', data => {
logServer(`FIN received: ${sock.remoteAddress} ${sock.remotePort}`);
});
sock.on('close', data => {
logServer(`CLOSED: ${sock.remoteAddress} ${sock.remotePort}`);
});
}
let count = 0;
server.on('connection', sock => {
count += 1;
const logServer = _logServer(`${count}`);
logServer(`CONNECTED: ${count}`);
setupSocket(sock, logServer, count == 3);
});
server.on('end', () => logServer('client disconnected'));
server.on('error', error => logServer(error));
// Client /////
const _logClient = (p) => (s) => console.log(`[Client:${p}]`, s);
const createClient = (name, destroy=true, host, port) => {
const logClient = _logClient(name);
const client = new net.Socket();
logClient('Created');
// Timeout after 3s of inactivity on socket
client.setTimeout(3000);
client.on('data', data => {
logClient('DATA: ' + data);
if(destroy) {
logClient('self destruct')
client.destroy();
}
});
client.on('end', () => {
logClient('Connection ended');
});
client.on('error', err => {
logClient(err);
});
client.on('timeout', () => {
logClient(`timed out`)
client.end();
});
client.on('close', () => {
logClient('Connection closed');
});
client.on('connect', () => {
logClient(`CONNECTED TO: ${host}:${port}`);
client.write('I am Chuck Norris!');
});
client.connect(port, host);
return client;
}
// Spawn server that we will terminate after client connects
const explode = spawn('node', ['self-destruct-server.js']);
explode.on('error', error => {
console.log(error);
});
setTimeout(() => {
console.log("Explode server");
explode.kill('SIGTERM');
}, 1500);
const c1 = createClient('c1', true, HOST, PORT);
const c2 = createClient('c2', false, HOST, PORT);
const c3 = createClient('c3', false, HOST, PORT);
// Note: Must wait ~ 20s for c4 to timeout
const c4 = createClient('c4', false, '12.34.56.78', 1234); // ETIMEDOUT
const c5 = createClient('c5', false, HOST, 8000); // ECONNRESET
const c6 = createClient('c6', false, HOST, 8001); // ECONNREFUSED
const net = require('net');
const { spawn } = require('child_process');
const HOST = '127.0.0.1';
const PORT = 6969;
// Server /////
const _logServer = (p) => (s) => console.log(`[Server:${p}]`, s);
const logServer = _logServer('main');
const server = net.createServer();
server.listen(PORT, HOST, () => {
const info = server.address();
logServer(`Server listening on ${info.address}:${info.port}`);
});
const setupSocket = (sock, logServer, shouldClose=false) => {
sock.on('data', data => {
logServer(`DATA ${sock.remoteAddress}:${data}`);
// Echo data back to the socket
sock.write(`You said "${data}"`);
if(shouldClose) {
logServer('Ending socket')
sock.end()
}
});
sock.on('end', data => {
logServer(`FIN received: ${sock.remoteAddress} ${sock.remotePort}`);
});
sock.on('close', data => {
logServer(`CLOSED: ${sock.remoteAddress} ${sock.remotePort}`);
});
}
let count = 0;
server.on('connection', sock => {
count += 1;
const logServer = _logServer(`${count}`);
logServer(`CONNECTED: ${count}`);
setupSocket(sock, logServer, count == 3);
});
server.on('end', () => logServer('client disconnected'));
server.on('error', error => logServer(error));
// Client /////
const _logClient = (p) => (s) => console.log(`[Client:${p}]`, s);
const createClient = (name, destroy=true, host, port) => {
const logClient = _logClient(name);
const client = new net.Socket();
logClient('Created');
// Timeout after 3s of inactivity on socket
client.setTimeout(3000);
client.on('data', data => {
logClient('DATA: ' + data);
if(destroy) {
logClient('self destruct')
client.destroy();
}
});
client.on('end', () => {
logClient('Connection ended');
});
client.on('error', err => {
logClient(err);
});
client.on('timeout', () => {
logClient(`timed out`)
client.end();
});
client.on('close', () => {
logClient('Connection closed');
});
client.on('connect', () => {
logClient(`CONNECTED TO: ${host}:${port}`);
client.write('I am Chuck Norris!');
});
client.connect(port, host);
return client;
}
// Spawn server that we will terminate after client connects
const explode = spawn('node', ['self-destruct-server.js']);
explode.on('error', error => {
console.log(error);
});
setTimeout(() => {
console.log("Explode server");
explode.kill('SIGTERM');
}, 1500);
const c1 = createClient('c1', true, HOST, PORT);
const c2 = createClient('c2', false, HOST, PORT);
const c3 = createClient('c3', false, HOST, PORT);
const c4 = createClient('c4', false, '12.34.56.78', 1234);
const c5 = createClient('c5', false, HOST, 8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment