Working much nicer

This commit is contained in:
Bryan Ashby 2020-05-22 12:55:52 -06:00
parent eeba609560
commit 2234a71705
No known key found for this signature in database
GPG Key ID: B49EB437951D2542
1 changed files with 10 additions and 75 deletions

View File

@ -27,24 +27,15 @@ const ModuleInfo = exports.moduleInfo = {
class WebSocketClient extends TelnetClient {
constructor(ws, req, serverType) {
//
// This bridge makes accessible various calls that client sub classes
// want to access on I/O socket
//
const socketBridge = new class SocketBridge extends Duplex {
// allow WebSocket to act like a Duplex (socket)
const wsDuplex = new class WebSocketDuplex extends Duplex {
constructor(ws) {
super();
this.ws = ws;
this.ws.on('close', err => this.emit('close', err));
//this.ws.on('connect', () => this.emit('connect'));
//this.ws.on('drain', () => this.emit('drain'));
//this.ws.on('end', () => this.emit('end'));
this.ws.on('error', err => this.emit('error', err));
//this.ws.on('ready', () => this.emit('ready'));
//this.ws.on('timeout', () => this.emit('timeout'));
this.ws.on('data', data => this._data(data));
this.ws.on('message', data => this._data(data));
}
setClient(client) {
@ -69,73 +60,22 @@ class WebSocketClient extends TelnetClient {
this.push(data);
}
}(ws);
// const socketBridge = new class SocketBridge extends Writable {
// constructor(ws) {
// super();
// this.ws = ws;
// }
// setClient(client) {
// this.client = client;
// }
super(wsDuplex);
wsDuplex.setClient(this);
// end() {
// return ws.close();
// }
// write(data, cb) {
// cb = cb || ( () => { /* eat it up */} ); // handle data writes after close
// return this.ws.send(data, { binary : true }, cb);
// }
// pipe(dest) {
// Log.trace('WebSocket SocketBridge pipe()');
// this.client.pipedDest = dest;
// }
// unpipe() {
// Log.trace('WebSocket SocketBridge unpipe()');
// this.client.pipedDest = null;
// }
// resume() {
// Log.trace('WebSocket SocketBridge resume()');
// }
// get remoteAddress() {
// // Support X-Forwarded-For and X-Real-IP headers for proxied connections
// return (this.client.proxied && (req.headers['x-forwarded-for'] || req.headers['x-real-ip'])) || req.connection.remoteAddress;
// }
// }(ws);
// :TODO: this is quite the clusterfuck...
super(socketBridge);
this.socketBridge = socketBridge;
this.serverType = serverType;
this.socketBridge.setClient(this);
this.dataHandler = function(data) {
if(this.pipedDest) {
this.pipedDest.write(data);
} else {
this.socketBridge.emit('data', data);
}
}.bind(this);
ws.on('message', this.dataHandler);
ws.on('close', () => {
wsDuplex.on('close', () => {
// we'll remove client connection which will in turn end() via our SocketBridge above
return this.emit('end');
});
this.serverType = serverType;
//
// Monitor connection status with ping/pong
//
ws.on('pong', () => {
Log.trace(`Pong from ${this.socketBridge.remoteAddress}`);
Log.trace(`Pong from ${wsDuplex.remoteAddress}`);
ws.isConnectionAlive = true;
});
@ -261,7 +201,7 @@ exports.getModule = class WebSocketLoginServer extends LoginServerModule {
server.wsServer.on('connection', (ws, req) => {
const webSocketClient = new WebSocketClient(ws, req, serverType);
this.handleNewClient(webSocketClient, webSocketClient.socketBridge, ModuleInfo);
this.handleNewClient(webSocketClient, webSocketClient.socket, ModuleInfo);
});
Log.info( { server : serverName, port : port }, 'Listening for connections' );
@ -272,9 +212,4 @@ exports.getModule = class WebSocketLoginServer extends LoginServerModule {
cb(err);
});
}
webSocketConnection(conn) {
const webSocketClient = new WebSocketClient(conn);
this.handleNewClient(webSocketClient, webSocketClient.socketShim, ModuleInfo);
}
};