Permalink
Browse files

Improve handling of connection errors in NodeHttpTransport.

  • Loading branch information...
1 parent 6bd79a5 commit abc5f3fff3f2847e4e9118137266f05eb393fc57 @jcoglan jcoglan committed Jul 14, 2010
Showing with 23 additions and 20 deletions.
  1. +1 −1 examples/node/pingpong.js
  2. +22 −19 javascript/network/node.js
@@ -1,7 +1,7 @@
var faye = require('./faye-node'),
sys = require('sys');
-ENDPOINT = 'http://localhost:8080/cometd';
+ENDPOINT = 'http://localhost:9292/bayeux';
sys.puts('Connecting to ' + ENDPOINT);
var ping = new Faye.Client(ENDPOINT);
View
@@ -1,10 +1,27 @@
Faye.NodeHttpTransport = Faye.Class(Faye.Transport, {
request: function(message, timeout) {
- var retry = this.retry(message, timeout),
- request = this.createRequestForMessage(message, retry),
- self = this;
+ var uri = url.parse(this._endpoint),
+ secure = (uri.protocol === 'https:'),
+ client = http.createClient(uri.port, uri.hostname, secure),
+ content = JSON.stringify(message),
+ response = null,
+ retry = this.retry(message, timeout),
+ self = this;
- request.addListener('response', function(response) {
+ client.addListener('error', retry);
+
+ client.addListener('end', function() {
+ if (!response) retry();
+ });
+
+ var request = client.request('POST', uri.pathname, {
+ 'Content-Type': 'application/json',
+ 'Host': uri.hostname,
+ 'Content-Length': content.length
+ });
+
+ request.addListener('response', function(stream) {
+ response = stream;
Faye.withDataFor(response, function(data) {
try {
self.receive(JSON.parse(data));
@@ -13,23 +30,9 @@ Faye.NodeHttpTransport = Faye.Class(Faye.Transport, {
}
});
});
- request.end();
- },
-
- createRequestForMessage: function(message, retry) {
- var content = JSON.stringify(message),
- uri = url.parse(this._endpoint),
- client = http.createClient(uri.port, uri.hostname, uri.protocol === 'https:');
-
- client.addListener('error', retry);
- var request = client.request('POST', uri.pathname, {
- 'Content-Type': 'application/json',
- 'host': uri.hostname,
- 'Content-Length': content.length
- });
request.write(content);
- return request;
+ request.end();
}
});

0 comments on commit abc5f3f

Please sign in to comment.