Skip to content

Commit

Permalink
fix error handler for WebSocket connections to call onerror with prop…
Browse files Browse the repository at this point in the history
…er event type. closes #213 and closes #214 issues
  • Loading branch information
wandenberg committed Dec 30, 2015
1 parent 3da6cb0 commit 7e67c0f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions misc/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ begin
require File.expand_path('misc/spec/spec_helper', project_dir)
include NginxTestHelper
template = File.read(File.expand_path('misc/nginx.conf', project_dir))
template.gsub!(/push_stream_subscriber_connection_ttl.*;/, 'push_stream_subscriber_connection_ttl 3s;')
template.gsub!(/push_stream_longpolling_connection_ttl.*;/, 'push_stream_longpolling_connection_ttl 3s;')
config = NginxTestHelper::Config.new "jasmine", {:configuration_template => (RUBY_PLATFORM =~ /darwin/) ? template.gsub('epoll', 'kqueue') : template }
abort "Could not start test server" if start_server(config).include?("[emerg]")
end
Expand Down
3 changes: 2 additions & 1 deletion misc/js/pushstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ Authors: Wandenberg Peixoto <wandenberg@gmail.com>, Rogério Carvalho Schneider
return;
}
this._closeCurrentConnection();
this.pushstream._onerror({type: ((event && ((event.type === "load") || (event.type === "close"))) || (this.pushstream.readyState === PushStream.CONNECTING)) ? "load" : "timeout"});
this.pushstream._onerror({type: ((event && ((event.type === "load") || ((event.type === "close") && (event.code === 1006)))) || (this.pushstream.readyState === PushStream.CONNECTING)) ? "load" : "timeout"});
};

/* wrappers */
Expand Down Expand Up @@ -732,6 +732,7 @@ Authors: Wandenberg Peixoto <wandenberg@gmail.com>, Rogério Carvalho Schneider

_onframeloaded: function() {
Log4js.info("[Stream] frame loaded (disconnected by server)");
this.pushstream._onerror({type: "timeout"});
this.connection.onload = null;
this.disconnect();
},
Expand Down
30 changes: 30 additions & 0 deletions misc/spec/javascripts/PushStreamSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,36 @@ describe("PushStream", function() {
});
});

if ((mode === "websocket") || (mode === "stream")) {
describe("when the connection timeout", function() {
it("should call onerror callback with a timeout error type", function(done) {
var error = null;
pushstream = new PushStream({
modes: mode,
port: port,
onerror: function(err) {
error = err;
}
});
pushstream.addChannel(channelName);

pushstream.connect();

waitsForAndRuns(
function() { return error !== null; },

function() {
expect(pushstream.readyState).toBe(PushStream.CLOSED);
expect(error.type).toBe("timeout");
done();
},

6000
);
});
});
}

describe("when adding a new channel", function() {
it("should reconnect", function(done) {
var status = [];
Expand Down

0 comments on commit 7e67c0f

Please sign in to comment.