From 1d3f44b0fd22c05afaae48c073b21cc5878bf30d Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 18 Feb 2017 16:51:11 +0900 Subject: [PATCH] :v: --- src/web/app/common/mixins.ls | 2 +- .../app/common/scripts/messaging-stream.js | 36 +++++++++++++++++++ .../app/common/scripts/messaging-stream.ls | 34 ------------------ 3 files changed, 37 insertions(+), 35 deletions(-) create mode 100644 src/web/app/common/scripts/messaging-stream.js delete mode 100644 src/web/app/common/scripts/messaging-stream.ls diff --git a/src/web/app/common/mixins.ls b/src/web/app/common/mixins.ls index 947fad553..119479399 100644 --- a/src/web/app/common/mixins.ls +++ b/src/web/app/common/mixins.ls @@ -15,7 +15,7 @@ module.exports = (me) ~> signout: require './scripts/signout.js' riot.mixin \messaging-stream do - MessagingStreamConnection: require './scripts/messaging-stream.ls' + MessagingStreamConnection: require './scripts/messaging-stream' riot.mixin \is-promise do is-promise: require './scripts/is-promise.ls' diff --git a/src/web/app/common/scripts/messaging-stream.js b/src/web/app/common/scripts/messaging-stream.js new file mode 100644 index 000000000..e6fc6f8bd --- /dev/null +++ b/src/web/app/common/scripts/messaging-stream.js @@ -0,0 +1,36 @@ +const ReconnectingWebSocket = require('reconnecting-websocket'); +const riot = require('riot'); + +class Connection { + constructor(me, otherparty) { + this.event = riot.observable(); + this.me = me; + + const host = CONFIG.api.url.replace('http', 'ws'); + this.socket = new ReconnectingWebSocket(`${host}/messaging?i=${me.token}&otherparty=${otherparty}`); + this.socket.addEventListener('open', this.onOpen); + this.socket.addEventListener('message', this.onMessage); + } + + onOpen() { + this.socket.send(JSON.stringify({ + i: this.me.token + })); + } + + onMessage(message) { + try { + const message = JSON.parse(message.data); + if (message.type) this.event.trigger(message.type, message.body); + } catch(e) { + // noop + } + } + + close() { + this.socket.removeEventListener('open', this.onOpen); + this.socket.removeEventListener('message', this.onMessage); + } +} + +module.exports = Connection; diff --git a/src/web/app/common/scripts/messaging-stream.ls b/src/web/app/common/scripts/messaging-stream.ls deleted file mode 100644 index ac3e74f1f..000000000 --- a/src/web/app/common/scripts/messaging-stream.ls +++ /dev/null @@ -1,34 +0,0 @@ -# Stream -#================================ - -ReconnectingWebSocket = require 'reconnecting-websocket' -riot = require 'riot' - -class Connection - (me, otherparty) ~> - @event = riot.observable! - @me = me - host = CONFIG.api.url.replace \http \ws - @socket = new ReconnectingWebSocket "#{host}/messaging?i=#{me.token}&otherparty=#{otherparty}" - - @socket.add-event-listener \open @on-open - @socket.add-event-listener \message @on-message - - on-open: ~> - @socket.send JSON.stringify do - i: @me.token - - on-message: (message) ~> - try - message = JSON.parse message.data - if message.type? - @event.trigger message.type, message.body - catch - # ignore - - close: ~> - @socket.remove-event-listener \open @on-open - @socket.remove-event-listener \message @on-message - @socket.close! - -module.exports = Connection