src: fix "send() is not a function" when enable mux feature though http proxy

This commit is contained in:
Micooz 2018-02-04 22:30:46 +08:00
parent db56c0119a
commit 12a21658b8
2 changed files with 16 additions and 5 deletions

@ -242,15 +242,21 @@ export class Hub {
}
};
let muxRelay = null;
let muxRelay = null, cid = null;
if (__MUX__) {
if (__IS_CLIENT__) {
// get or create a mux relay
muxRelay = this.getMuxRelay() || this._createMuxRelay(context);
if (!muxRelay.isOutboundReady()) {
muxRelay.init({proxyRequest});
if (muxRelay.isOutboundReady()) {
proxyRequest.onConnected((buffer) => {
// this callback is used for "http" proxy method on client side
if (buffer) {
muxRelay.encode(buffer, {...proxyRequest, cid});
}
});
} else {
proxyRequest.onConnected();
proxyRequest.cid = cid;
muxRelay.init({proxyRequest});
}
// add mux relay instance to context
Object.assign(context, {muxRelay});

@ -327,7 +327,12 @@ export class TcpOutbound extends Outbound {
}
this._socket.on('connect', () => {
if (typeof onConnected === 'function') {
onConnected(this._inbound.onReceive);
onConnected((buffer) => {
if (buffer) {
const type = __IS_CLIENT__ ? PIPE_ENCODE : PIPE_DECODE;
this.ctx.pipe.feed(type, buffer, {cid: this.ctx.proxyRequest.cid, host, port});
}
});
}
this.ctx.pipe.broadcast(null, {type: CONNECTED_TO_REMOTE, payload: {host, port}});
});