Initial hacks to get the split to work
This commit is contained in:
parent
10d4ec8e60
commit
b536c15758
6 changed files with 68 additions and 8 deletions
|
|
@ -1,20 +1,62 @@
|
|||
import ioServer from '../ioserver';
|
||||
import ProxiedSocket from './proxiedsocket';
|
||||
|
||||
export default class FrontendManager {
|
||||
constructor() {
|
||||
constructor(socketEmitter) {
|
||||
this.socketEmitter = socketEmitter;
|
||||
this.frontendConnections = {};
|
||||
this.frontendProxiedSockets = {};
|
||||
}
|
||||
|
||||
onConnection(socket) {
|
||||
if (this.frontendConnections.hasOwnProperty(socket.remoteAddress)) {
|
||||
if (this.frontendConnections.hasOwnProperty(socket.remoteAddressAndPort)) {
|
||||
// TODO: do some validation, maybe check if the socket is still connected?
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
this.frontendConnections[socket.remoteAddressAndPort] = socket;
|
||||
console.log(socket.remoteAddressAndPort);
|
||||
socket.on('data', this.onData.bind(this, socket));
|
||||
}
|
||||
|
||||
onData(socket, data) {
|
||||
console.log(data);
|
||||
switch (data.$type) {
|
||||
case 'socketConnect':
|
||||
this.onSocketConnect(socket, data);
|
||||
break;
|
||||
case 'socketFrame':
|
||||
this.onSocketFrame(socket, data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
onSocketConnect(frontendConnection, data) {
|
||||
const mapKey = frontendConnection.remoteAddressAndPort;
|
||||
const proxiedSocket = new ProxiedSocket(
|
||||
data.socketID,
|
||||
data.socketData,
|
||||
this.socketEmitter,
|
||||
frontendConnection);
|
||||
|
||||
if (!this.frontendProxiedSockets.hasOwnProperty(mapKey)) {
|
||||
this.frontendProxiedSockets[mapKey] = {};
|
||||
} else if (this.frontendProxiedSockets[mapKey].hasOwnProperty(data.socketID)) {
|
||||
// TODO: Handle this gracefully
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
this.frontendProxiedSockets[mapKey][data.socketID] = proxiedSocket;
|
||||
ioServer.handleConnection(proxiedSocket);
|
||||
}
|
||||
|
||||
onSocketFrame(frontendConnection, data) {
|
||||
const mapKey = frontendConnection.remoteAddressAndPort;
|
||||
const socketMap = this.frontendProxiedSockets[mapKey];
|
||||
if (!socketMap || !socketMap.hasOwnProperty(data.socketID)) {
|
||||
// TODO
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
const socket = socketMap[data.socketID];
|
||||
socket.onProxiedEventReceived.apply(socket, [data.event].concat(data.args));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,14 +2,15 @@ import Server from 'cytube-common/lib/tcpjson/server';
|
|||
import FrontendManager from './frontendmanager';
|
||||
|
||||
export default class IOBackend {
|
||||
constructor(proxyListenerConfig) {
|
||||
constructor(proxyListenerConfig, socketEmitter) {
|
||||
this.proxyListenerConfig = proxyListenerConfig;
|
||||
this.socketEmitter = socketEmitter;
|
||||
this.initFrontendManager();
|
||||
this.initProxyListener();
|
||||
}
|
||||
|
||||
initFrontendManager() {
|
||||
this.frontendManager = new FrontendManager();
|
||||
this.frontendManager = new FrontendManager(this.socketEmitter);
|
||||
}
|
||||
|
||||
initProxyListener() {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ export default class ProxiedSocket extends EventEmitter {
|
|||
}
|
||||
|
||||
emit() {
|
||||
const target = socketEmitter.to(this.id);
|
||||
const target = this.socketEmitter.to(this.id);
|
||||
target.emit.apply(target, arguments);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -273,7 +273,9 @@ module.exports = {
|
|||
|
||||
bound[id] = null;
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
handleConnection: handleConnection
|
||||
};
|
||||
|
||||
/* Clean out old rate limiters */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue