diff --git a/src/streaming.ts b/src/streaming.ts index 92b8319..6e30115 100644 --- a/src/streaming.ts +++ b/src/streaming.ts @@ -54,6 +54,7 @@ export default class Stream extends EventEmitter { 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 @@ -134,6 +135,17 @@ export default class Stream extends EventEmitter { } } + /** + * 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 */ @@ -182,7 +194,10 @@ export default class Stream extends EventEmitter { */ @autobind public close(): void { - this.stream.close(); + // this.stream.readyState が CONNECTING とかのときにCLOSEすると死ぬ + if (this.stream.readyState === WebSocket.OPEN ) { + this.stream.close(); + } } }