Working much nicer
This commit is contained in:
parent
eeba609560
commit
2234a71705
|
@ -27,24 +27,15 @@ const ModuleInfo = exports.moduleInfo = {
|
||||||
|
|
||||||
class WebSocketClient extends TelnetClient {
|
class WebSocketClient extends TelnetClient {
|
||||||
constructor(ws, req, serverType) {
|
constructor(ws, req, serverType) {
|
||||||
//
|
// allow WebSocket to act like a Duplex (socket)
|
||||||
// This bridge makes accessible various calls that client sub classes
|
const wsDuplex = new class WebSocketDuplex extends Duplex {
|
||||||
// want to access on I/O socket
|
|
||||||
//
|
|
||||||
const socketBridge = new class SocketBridge extends Duplex {
|
|
||||||
constructor(ws) {
|
constructor(ws) {
|
||||||
super();
|
super();
|
||||||
this.ws = ws;
|
this.ws = ws;
|
||||||
|
|
||||||
this.ws.on('close', err => this.emit('close', err));
|
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('error', err => this.emit('error', err));
|
||||||
|
this.ws.on('message', data => this._data(data));
|
||||||
//this.ws.on('ready', () => this.emit('ready'));
|
|
||||||
//this.ws.on('timeout', () => this.emit('timeout'));
|
|
||||||
this.ws.on('data', data => this._data(data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setClient(client) {
|
setClient(client) {
|
||||||
|
@ -69,73 +60,22 @@ class WebSocketClient extends TelnetClient {
|
||||||
this.push(data);
|
this.push(data);
|
||||||
}
|
}
|
||||||
}(ws);
|
}(ws);
|
||||||
// const socketBridge = new class SocketBridge extends Writable {
|
|
||||||
// constructor(ws) {
|
|
||||||
// super();
|
|
||||||
// this.ws = ws;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// setClient(client) {
|
super(wsDuplex);
|
||||||
// this.client = client;
|
wsDuplex.setClient(this);
|
||||||
// }
|
|
||||||
|
|
||||||
// end() {
|
wsDuplex.on('close', () => {
|
||||||
// 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', () => {
|
|
||||||
// we'll remove client connection which will in turn end() via our SocketBridge above
|
// we'll remove client connection which will in turn end() via our SocketBridge above
|
||||||
return this.emit('end');
|
return this.emit('end');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.serverType = serverType;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Monitor connection status with ping/pong
|
// Monitor connection status with ping/pong
|
||||||
//
|
//
|
||||||
ws.on('pong', () => {
|
ws.on('pong', () => {
|
||||||
Log.trace(`Pong from ${this.socketBridge.remoteAddress}`);
|
Log.trace(`Pong from ${wsDuplex.remoteAddress}`);
|
||||||
ws.isConnectionAlive = true;
|
ws.isConnectionAlive = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -261,7 +201,7 @@ exports.getModule = class WebSocketLoginServer extends LoginServerModule {
|
||||||
|
|
||||||
server.wsServer.on('connection', (ws, req) => {
|
server.wsServer.on('connection', (ws, req) => {
|
||||||
const webSocketClient = new WebSocketClient(ws, req, serverType);
|
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' );
|
Log.info( { server : serverName, port : port }, 'Listening for connections' );
|
||||||
|
@ -272,9 +212,4 @@ exports.getModule = class WebSocketLoginServer extends LoginServerModule {
|
||||||
cb(err);
|
cb(err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
webSocketConnection(conn) {
|
|
||||||
const webSocketClient = new WebSocketClient(conn);
|
|
||||||
this.handleNewClient(webSocketClient, webSocketClient.socketShim, ModuleInfo);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue