Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

Add support of connection error event #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions EventSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ var EventSource = function(url, options) {
} else if (eventsource.readyState !== eventsource.CLOSED) {
if (this.readyState == 4) {
// and some other status
eventsource.dispatchEvent('connection-error', {
type: 'connection-error',
status: this.status,
message: this.responseText,
});
pollAgain(interval);
} else if (this.readyState == 0) {
// likely aborted
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class MyApp extends Component {
console.log(data.type); // message
console.log(data.data);
});

// Grab connection error events with the type of 'connection-error'
this.eventSource.addEventListener('connection-error', (data) => {
console.log(data.message); // message
console.log(data.status); // http status code
});
}
componentWillUnmount() {
this.eventSource.removeAllListeners();
Expand Down