Comparing version 0.0.1 to 0.0.2
@@ -27,2 +27,9 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
/** | ||
* @ 代理错误处理函数 | ||
*/ | ||
this._whenProxyError = function (error, client) { | ||
client.end(); | ||
}; | ||
/** | ||
* @ 协议解析函数 | ||
@@ -58,8 +65,14 @@ */ | ||
var onerror = function (err) { | ||
_that._whenProxyError(err, client); | ||
}; | ||
var socket = net.createConnection(R, function () { | ||
socket.write(H); | ||
client.pipe(socket); | ||
socket.pipe(client); | ||
this.removeListener('error', onerror); | ||
this.write(H); | ||
client.pipe(this); | ||
this.pipe(client); | ||
client.resume(); | ||
}); | ||
socket.once('error', onerror); | ||
} | ||
@@ -76,2 +89,8 @@ }); | ||
ProxyServer.prototype.whenProxyError = function (handle) { | ||
if ('function' === (typeof handle)) { | ||
this._whenProxyError = handle; | ||
} | ||
}; | ||
ProxyServer.prototype.listen = function () { | ||
@@ -78,0 +97,0 @@ this.server.listen.apply(this.server, arguments); |
@@ -5,3 +5,3 @@ { | ||
"keywords": [ "tcp proxy" ], | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"author": "Aleafs Zhang (zhangxc83@gmail.com)", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -70,3 +70,7 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
var proxy = sproxy.createServer(); | ||
proxy.use(function (buffer) { | ||
proxy.whenProxyError(function (error, client) { | ||
client.end(error.stack); | ||
}); | ||
proxy.use(function (buffer, socket) { | ||
buffer = buffer.toString(); | ||
@@ -85,2 +89,6 @@ if (buffer.indexOf('hello') > -1) { | ||
} | ||
return { | ||
'path' : 'i/am/not/exists', | ||
}; | ||
}); | ||
@@ -91,3 +99,3 @@ | ||
var count = 2; | ||
var count = 3; | ||
var close = function () { | ||
@@ -118,2 +126,10 @@ if (0 === (--count)) { | ||
}); | ||
var c3 = net.createConnection(_FD, function () { | ||
this.write('test error'); | ||
this.on('data', function (data) { | ||
data.toString().should.include('Error: connect ENOENT'); | ||
close(); | ||
}); | ||
}); | ||
}); | ||
@@ -120,0 +136,0 @@ }); |
8373
201