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

streaming のエラーハンドリングをもうちょっと #58

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion src/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
this.stream.addEventListener('open', this.onOpen);
this.stream.addEventListener('close', this.onClose);
this.stream.addEventListener('message', this.onMessage);
this.stream.addEventListener('error', console.error);
}

@autobind
Expand Down Expand Up @@ -134,6 +135,17 @@ export default class Stream extends EventEmitter<StreamEvents> {
}
}

/**
* Callback of when error connection
*/
@autobind
private onError(): void {
if (this.state === 'connected') {
this.state = 'reconnecting';
this.emit('_disconnected_');
}
}

/**
* Callback of when received a message from connection
*/
Expand Down Expand Up @@ -182,7 +194,10 @@ export default class Stream extends EventEmitter<StreamEvents> {
*/
@autobind
public close(): void {
this.stream.close();
// this.stream.readyState が CONNECTING とかのときにCLOSEすると死ぬ
if (this.stream.readyState === WebSocket.OPEN ) {
this.stream.close();
}
}
}

Expand Down