Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you're probably after something like this:</p> <pre><code>/*jslint node: true, white: true */ // Declare constructors var DataSource, PHPClientServer; // The DataSource class // Handles connecting/reconnecting to the data source, and piping endpoints together (function() { "use strict"; DataSource = function(net) { this.net = net; }; DataSource.prototype.net = null; DataSource.prototype.host = 'localhost'; DataSource.prototype.port = 0; DataSource.prototype.user = ''; DataSource.prototype.pass = ''; DataSource.prototype.socket = null; DataSource.prototype.currentClient = null; DataSource.prototype.start = function(host, port, user, pass) { if (host !== undefined) { this.host = host; } if (port !== undefined) { this.port = port; } if (user !== undefined) { this.user = user; } if (pass !== undefined) { this.pass = pass; } this.socket = this.net.createConnection(this.port, this.host); this.socket.on('connect', function () { console.log('Data source connected'); this.socket.write(this.user + '@' + this.pass); }.bind(this)); this.socket.on('error', function() { console.error('Error on data source connection'); this.stop(); this.start(); }.bind(this)); this.socket.on('end', function() { console.error('Data source connection terminated'); this.stop(); this.start(); }.bind(this)); }; DataSource.prototype.stop = function() { this.socket.end(); this.socket = null; }; DataSource.prototype.attachClient = function(client) { console.log('Attaching client to data source'); this.currentClient = client; this.socket.pipe(this.currentClient); this.currentClient.pipe(this.socket, {end: false}); }; DataSource.prototype.detachCurrentClient = function() { console.log('Detaching client from data source'); this.socket.unpipe(this.currentClient); this.currentClient.unpipe(this.socket); this.currentClient = null; }; DataSource.prototype.hasClient = function() { return this.currentClient !== null; }; }()); // The PHPClientServer class // Handles the server operations for PHP clients (function() { "use strict"; PHPClientServer = function(net, dataSource) { this.net = net; this.dataSource = dataSource; this.pendingClientStack = []; }; PHPClientServer.prototype.net = null; PHPClientServer.prototype.dataSource = null; PHPClientServer.prototype.host = null; PHPClientServer.prototype.port = null; PHPClientServer.prototype.server = null; PHPClientServer.prototype.pendingClientStack = null; PHPClientServer.prototype.start = function(host, port) { var clientTerminateHandler = function() { console.log('Client disconnected'); this.dataSource.detachCurrentClient(); if (this.pendingClientStack.length) { console.log('Attaching next client in queue'); this.dataSource.attachClient(this.pendingClientStack.shift()); } }.bind(this); if (host !== undefined) { this.host = host; } if (port !== undefined) { this.port = port; } this.server = this.net.createServer(function(client) { console.log('Client connected'); client.on('end', clientTerminateHandler); client.on('error', clientTerminateHandler); if (this.dataSource.hasClient()) { console.log('Client added to queue'); this.pendingClientStack.push(client); } else { this.dataSource.attachClient(client); } }.bind(this)); this.server.listen(this.port, this.host); }; PHPClientServer.prototype.stop = function() { this.server.close(); this.server = null; }; }()); // Bootstrap var net, dataSource, server; net = require('net'); dataSource = new DataSource(net); dataSource.start('192.168.0.1', 23); server = new PHPClientServer(net, dataSource); server.start('0.0.0.0', 12345); </code></pre> <p>I realise that's a wall of code with minimal explanation, so please ask if there's something you don't understand.</p> <p>Also, before anyone says it, yes I am fully aware that I am treating a prototypical OOP language as if it were a classical one, Javascript != Java, yada yada yada. I don't care, I like to work with Javascript in this manner.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload