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;
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) {
switch (data.$type) {
case 'socketConnect':
this.onSocketConnect(socket, data);
break;
case 'socketFrame':
this.onSocketFrame(socket, data);
break;
}
}
onSocketConnect(frontendConnection, data) {
onSocketConnect(frontendConnection, socketID, socketIP) {
const mapKey = frontendConnection.endpoint;
const proxiedSocket = new ProxiedSocket(
data.socketID,
data.socketData,
socketID,
socketIP,
this.socketEmitter,
frontendConnection);
if (!this.frontendProxiedSockets.hasOwnProperty(mapKey)) {
this.frontendProxiedSockets[mapKey] = {};
} else if (this.frontendProxiedSockets[mapKey].hasOwnProperty(data.socketID)) {
} else if (this.frontendProxiedSockets[mapKey].hasOwnProperty(socketID)) {
// TODO: Handle this gracefully
throw new Error();
}
this.frontendProxiedSockets[mapKey][data.socketID] = proxiedSocket;
this.frontendProxiedSockets[mapKey][socketID] = proxiedSocket;
ioServer.handleConnection(proxiedSocket);
}
onSocketFrame(frontendConnection, data) {
onSocketFrame(frontendConnection, socketID, event, args) {
const mapKey = frontendConnection.endpoint;
const socketMap = this.frontendProxiedSockets[mapKey];
if (!socketMap || !socketMap.hasOwnProperty(data.socketID)) {
if (!socketMap || !socketMap.hasOwnProperty(socketID)) {
// TODO
throw new Error();
}
const socket = socketMap[data.socketID];
socket.onProxiedEventReceived.apply(socket, [data.event].concat(data.args));
const socket = socketMap[socketID];
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';
export default class IOBackend {

View File

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