summaryrefslogtreecommitdiff
path: root/src/web
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-05-11 05:08:12 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-05-11 05:08:12 +0900
commitc3cef31974d4768ee4bea07f8b67e1a6f4331ed0 (patch)
tree971b1e6fbecab154b2e1a62e2d253e8421e06f62 /src/web
parentUpdate boot.js (diff)
downloadsharkey-c3cef31974d4768ee4bea07f8b67e1a6f4331ed0.tar.gz
sharkey-c3cef31974d4768ee4bea07f8b67e1a6f4331ed0.tar.bz2
sharkey-c3cef31974d4768ee4bea07f8b67e1a6f4331ed0.zip
Refactor
Diffstat (limited to 'src/web')
-rw-r--r--src/web/app/common/scripts/stream.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/web/app/common/scripts/stream.js b/src/web/app/common/scripts/stream.js
index f6eaef3723..ac3dd67153 100644
--- a/src/web/app/common/scripts/stream.js
+++ b/src/web/app/common/scripts/stream.js
@@ -1,7 +1,12 @@
+'use strict';
+
const ReconnectingWebSocket = require('reconnecting-websocket');
import * as riot from 'riot';
import CONFIG from './config';
+/**
+ * Home stream connection
+ */
class Connection {
constructor(me) {
// BIND -----------------------------------
@@ -27,6 +32,10 @@ class Connection {
this.on('i_updated', me.update);
}
+ /**
+ * Callback of when open connection
+ * @private
+ */
onOpen() {
this.state = 'connected';
this.trigger('_connected_');
@@ -39,11 +48,19 @@ class Connection {
});
}
+ /**
+ * Callback of when close connection
+ * @private
+ */
onClose() {
this.state = 'reconnecting';
this.trigger('_closed_');
}
+ /**
+ * Callback of when received a message from connection
+ * @private
+ */
onMessage(message) {
try {
const msg = JSON.parse(message.data);
@@ -53,6 +70,10 @@ class Connection {
}
}
+ /**
+ * Send a message to connection
+ * @public
+ */
send(message) {
// まだ接続が確立されていなかったらバッファリングして次に接続した時に送信する
if (this.state != 'connected') {
@@ -63,6 +84,10 @@ class Connection {
this.socket.send(JSON.stringify(message));
}
+ /**
+ * Close this connection
+ * @public
+ */
close() {
this.socket.removeEventListener('open', this.onOpen);
this.socket.removeEventListener('message', this.onMessage);