Browse Source

Add "had_error" argument to the "onDisconnect" in node.tcp.Client

This is a boolean value which allows one to detect if the socket was closed
due to errors. There is not yet a way to look up the actual error code.
v0.7.4-release
Ryan 16 years ago
parent
commit
8cfdd326a8
  1. 18
      src/net.cc
  2. 3
      test/test-pingpong.js
  3. 17
      test/test-reconnecting-socket.js
  4. 9
      website/api.html

18
src/net.cc

@ -422,6 +422,23 @@ Connection::OnReceive (const void *buf, size_t len)
FatalException(try_catch);
}
void
Connection::OnDisconnect ()
{
HandleScope scope;
Local<Value> callback_v = handle_->Get(ON_DISCONNECT_SYMBOL);
if (!callback_v->IsFunction()) return;
Handle<Function> callback = Handle<Function>::Cast(callback_v);
Handle<Value> argv[1];
argv[0] = socket_.errorno == 0 ? False() : True();
TryCatch try_catch;
callback->Call(handle_, 1, argv);
if (try_catch.HasCaught())
node::FatalException(try_catch);
}
#define DEFINE_SIMPLE_CALLBACK(name, symbol) \
void name () \
{ \
@ -437,7 +454,6 @@ void name () \
DEFINE_SIMPLE_CALLBACK(Connection::OnConnect, ON_CONNECT_SYMBOL)
DEFINE_SIMPLE_CALLBACK(Connection::OnDrain, ON_DRAIN_SYMBOL)
DEFINE_SIMPLE_CALLBACK(Connection::OnDisconnect, ON_DISCONNECT_SYMBOL)
DEFINE_SIMPLE_CALLBACK(Connection::OnTimeout, ON_TIMEOUT_SYMBOL)
DEFINE_SIMPLE_CALLBACK(Connection::OnEOF, ON_EOF_SYMBOL)

3
test/test-pingpong.js

@ -26,7 +26,8 @@ function Ponger (socket) {
socket.close();
};
socket.onDisconnect = function () {
socket.onDisconnect = function (had_error) {
assertFalse(had_error);
assertEquals("closed", socket.readyState);
puts("ponger: onDisconnect");
socket.server.close();

17
test/test-reconnecting-socket.js

@ -3,7 +3,7 @@ var port = 8921;
function onLoad () {
new node.tcp.Server(function (socket) {
var server = new node.tcp.Server(function (socket) {
puts("new connection");
socket.onConnect = function () {
socket.send("hello\r\n");
@ -13,10 +13,12 @@ function onLoad () {
socket.close();
};
socket.onDisconnect = function () {
socket.server.close();
socket.onDisconnect = function (had_error) {
//puts("server had_error: " + JSON.stringify(had_error));
assertFalse(had_error);
};
}).listen(port);
});
server.listen(port);
var count = 0;
var client = new node.tcp.Connection();
@ -32,10 +34,13 @@ function onLoad () {
client.fullClose();
};
client.onDisconnect = function () {
client.onDisconnect = function (had_error) {
assertFalse(had_error);
puts("client disconnected");
if (count++ < 5)
client.connect(port);
client.connect(port); // reconnect
else
server.close();
};
client.connect(port);

9
website/api.html

@ -398,8 +398,13 @@ server.listen(7000, "localhost");
You should probably just call <code>connection.close()</code> in this
callback.
<dt><code>conneciton.onDisconnect = function () { };</code></dt>
<dd>Called once the connection is fully disconnected.</dd>
<dt><code>conneciton.onDisconnect = function (had_error) { };</code></dt>
<dd>Called once the connection is fully disconnected.
<p>The callback is passed one boolean argument <code>had_error</code>.
This lets one know if the connect was closed due to an error. (TODO: look
up error codes.)
</dd>
<dt><code>conneciton.onError = function () { };</code></dt>
<dd>Called on an error.</dd>

Loading…
Cancel
Save