Use new protocol

This commit is contained in:
calzoneman 2015-12-28 23:52:39 -08:00
parent 9dd617d9fc
commit 5b44117681
3 changed files with 17 additions and 27 deletions

View File

@ -15,48 +15,38 @@ export default class FrontendManager {
} }
this.frontendConnections[socket.endpoint] = socket; this.frontendConnections[socket.endpoint] = socket;
socket.on('data', this.onData.bind(this, socket)); socket.on('SocketConnectEvent', this.onSocketConnect.bind(this, socket));
socket.on('SocketFrameEvent', this.onSocketFrame.bind(this, socket));
} }
onData(socket, data) { onSocketConnect(frontendConnection, socketID, socketIP) {
switch (data.$type) {
case 'socketConnect':
this.onSocketConnect(socket, data);
break;
case 'socketFrame':
this.onSocketFrame(socket, data);
break;
}
}
onSocketConnect(frontendConnection, data) {
const mapKey = frontendConnection.endpoint; const mapKey = frontendConnection.endpoint;
const proxiedSocket = new ProxiedSocket( const proxiedSocket = new ProxiedSocket(
data.socketID, socketID,
data.socketData, socketIP,
this.socketEmitter, this.socketEmitter,
frontendConnection); frontendConnection);
if (!this.frontendProxiedSockets.hasOwnProperty(mapKey)) { if (!this.frontendProxiedSockets.hasOwnProperty(mapKey)) {
this.frontendProxiedSockets[mapKey] = {}; this.frontendProxiedSockets[mapKey] = {};
} else if (this.frontendProxiedSockets[mapKey].hasOwnProperty(data.socketID)) { } else if (this.frontendProxiedSockets[mapKey].hasOwnProperty(socketID)) {
// TODO: Handle this gracefully // TODO: Handle this gracefully
throw new Error(); throw new Error();
} }
this.frontendProxiedSockets[mapKey][data.socketID] = proxiedSocket; this.frontendProxiedSockets[mapKey][socketID] = proxiedSocket;
ioServer.handleConnection(proxiedSocket); ioServer.handleConnection(proxiedSocket);
} }
onSocketFrame(frontendConnection, data) { onSocketFrame(frontendConnection, socketID, event, args) {
const mapKey = frontendConnection.endpoint; const mapKey = frontendConnection.endpoint;
const socketMap = this.frontendProxiedSockets[mapKey]; const socketMap = this.frontendProxiedSockets[mapKey];
if (!socketMap || !socketMap.hasOwnProperty(data.socketID)) { if (!socketMap || !socketMap.hasOwnProperty(socketID)) {
// TODO // TODO
throw new Error(); throw new Error();
} }
const socket = socketMap[data.socketID]; const socket = socketMap[socketID];
socket.onProxiedEventReceived.apply(socket, [data.event].concat(data.args)); socket.onProxiedEventReceived.apply(socket, [event].concat(args));
} }
} }

View File

@ -1,4 +1,4 @@
import Server from 'cytube-common/lib/tcpjson/server'; import Server from 'cytube-common/lib/proxy/server';
import FrontendManager from './frontendmanager'; import FrontendManager from './frontendmanager';
export default class IOBackend { export default class IOBackend {

View File

@ -1,11 +1,11 @@
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
export default class ProxiedSocket extends EventEmitter { export default class ProxiedSocket extends EventEmitter {
constructor(socketID, socketData, socketEmitter, frontendConnection) { constructor(socketID, socketIP, socketEmitter, frontendConnection) {
super(); super();
this.id = socketID; this.id = socketID;
this.ip = socketData.ip; this.ip = socketIP;
this._realip = socketData.ip; this._realip = socketIP;
this.socketEmitter = socketEmitter; this.socketEmitter = socketEmitter;
this.frontendConnection = frontendConnection; this.frontendConnection = frontendConnection;
} }
@ -21,7 +21,7 @@ export default class ProxiedSocket extends EventEmitter {
join(channel) { join(channel) {
this.frontendConnection.write( this.frontendConnection.write(
this.frontendConnection.protocol.socketJoinSocketChannels( this.frontendConnection.protocol.newSocketJoinRoomsEvent(
this.id, [channel] this.id, [channel]
) )
); );
@ -29,7 +29,7 @@ export default class ProxiedSocket extends EventEmitter {
disconnect() { disconnect() {
this.frontendConnection.write( this.frontendConnection.write(
this.frontendConnection.protocol.socketKick(this.id) this.frontendConnection.protocol.newSocketKickEvent(this.id)
); );
} }
} }