Initial hacks to get the split to work

This commit is contained in:
calzoneman 2015-12-26 15:07:03 -08:00
parent 10d4ec8e60
commit b536c15758
6 changed files with 68 additions and 8 deletions

View File

@ -34,6 +34,7 @@
"sanitize-html": "git://github.com/calzoneman/sanitize-html", "sanitize-html": "git://github.com/calzoneman/sanitize-html",
"serve-static": "^1.10.0", "serve-static": "^1.10.0",
"socket.io": "^1.3.7", "socket.io": "^1.3.7",
"socket.io-redis": "^1.0.0",
"source-map-support": "^0.3.2", "source-map-support": "^0.3.2",
"status-message-polyfill": "calzoneman/status-message-polyfill", "status-message-polyfill": "calzoneman/status-message-polyfill",
"yamljs": "^0.1.6" "yamljs": "^0.1.6"

View File

@ -1,20 +1,62 @@
import ioServer from '../ioserver';
import ProxiedSocket from './proxiedsocket';
export default class FrontendManager { export default class FrontendManager {
constructor() { constructor(socketEmitter) {
this.socketEmitter = socketEmitter;
this.frontendConnections = {}; this.frontendConnections = {};
this.frontendProxiedSockets = {};
} }
onConnection(socket) { 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? // TODO: do some validation, maybe check if the socket is still connected?
throw new Error(); throw new Error();
} }
this.frontendConnections[socket.remoteAddressAndPort] = socket; this.frontendConnections[socket.remoteAddressAndPort] = socket;
console.log(socket.remoteAddressAndPort);
socket.on('data', this.onData.bind(this, socket)); socket.on('data', this.onData.bind(this, socket));
} }
onData(socket, data) { 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));
} }
} }

View File

@ -2,14 +2,15 @@ import Server from 'cytube-common/lib/tcpjson/server';
import FrontendManager from './frontendmanager'; import FrontendManager from './frontendmanager';
export default class IOBackend { export default class IOBackend {
constructor(proxyListenerConfig) { constructor(proxyListenerConfig, socketEmitter) {
this.proxyListenerConfig = proxyListenerConfig; this.proxyListenerConfig = proxyListenerConfig;
this.socketEmitter = socketEmitter;
this.initFrontendManager(); this.initFrontendManager();
this.initProxyListener(); this.initProxyListener();
} }
initFrontendManager() { initFrontendManager() {
this.frontendManager = new FrontendManager(); this.frontendManager = new FrontendManager(this.socketEmitter);
} }
initProxyListener() { initProxyListener() {

View File

@ -11,7 +11,7 @@ export default class ProxiedSocket extends EventEmitter {
} }
emit() { emit() {
const target = socketEmitter.to(this.id); const target = this.socketEmitter.to(this.id);
target.emit.apply(target, arguments); target.emit.apply(target, arguments);
} }

View File

@ -273,7 +273,9 @@ module.exports = {
bound[id] = null; bound[id] = null;
}); });
} },
handleConnection: handleConnection
}; };
/* Clean out old rate limiters */ /* Clean out old rate limiters */

View File

@ -127,6 +127,20 @@ var Server = function () {
}); });
require("./io/ioserver").init(self, webConfig); require("./io/ioserver").init(self, webConfig);
const redisAdapter = require('socket.io-redis');
const IOBackend = require('./io/backend/iobackend');
const sioEmitter = require("socket.io").instance;
sioEmitter.adapter(redisAdapter());
const listenerConfig = {
getPort: function () {
return 3071;
},
getHost: function () {
return '127.0.0.1';
}
};
const backend = new IOBackend(listenerConfig, sioEmitter);
// background tasks init ---------------------------------------------- // background tasks init ----------------------------------------------
require("./bgtask")(self); require("./bgtask")(self);