[Client] Implement streaming buffering
This commit is contained in:
parent
4968efe101
commit
48386a8f68
1 changed files with 14 additions and 2 deletions
|
@ -16,6 +16,7 @@ class Connection {
|
||||||
|
|
||||||
this.state = 'initializing';
|
this.state = 'initializing';
|
||||||
this.me = me;
|
this.me = me;
|
||||||
|
this.buffer = [];
|
||||||
|
|
||||||
const host = CONFIG.apiUrl.replace('http', 'ws');
|
const host = CONFIG.apiUrl.replace('http', 'ws');
|
||||||
this.socket = new ReconnectingWebSocket(`${host}?i=${me.token}`);
|
this.socket = new ReconnectingWebSocket(`${host}?i=${me.token}`);
|
||||||
|
@ -29,6 +30,13 @@ class Connection {
|
||||||
onOpen() {
|
onOpen() {
|
||||||
this.state = 'connected';
|
this.state = 'connected';
|
||||||
this.trigger('_connected_');
|
this.trigger('_connected_');
|
||||||
|
|
||||||
|
// バッファーを処理
|
||||||
|
const _buffer = [].concat(this.buffer); // Shallow copy
|
||||||
|
this.buffer = []; // Clear buffer
|
||||||
|
_buffer.forEach(message => {
|
||||||
|
this.send(message); // Resend each buffered messages
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onClose() {
|
onClose() {
|
||||||
|
@ -46,8 +54,12 @@ class Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
send(message) {
|
send(message) {
|
||||||
// TODO: バッファリングしてつぎ接続した時に送信する
|
// まだ接続が確立されていなかったらバッファリングして次に接続した時に送信する
|
||||||
if (this.state != 'connected') return;
|
if (this.state != 'connected') {
|
||||||
|
this.buffer.push(message);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
this.socket.send(JSON.stringify(message));
|
this.socket.send(JSON.stringify(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue