|
| 1 | +import Vue from 'vue' |
| 2 | +import SockJS from 'sockjs-client' |
| 3 | +import globalBus from '@/event-bus' |
| 4 | + |
| 5 | +class RealTimeClient { |
| 6 | + constructor () { |
| 7 | + this.serverUrl = null |
| 8 | + this.token = null |
| 9 | + this.socket = null |
| 10 | + // If the client is authenticated through real time connection or not |
| 11 | + this.authenticated = false |
| 12 | + this.loggedOut = false |
| 13 | + this.$bus = new Vue() |
| 14 | + this.subscribeQueue = { |
| 15 | + /* channel: [handler1, handler2] */ |
| 16 | + } |
| 17 | + this.unsubscribeQueue = { |
| 18 | + /* channel: [handler1, handler2] */ |
| 19 | + } |
| 20 | + } |
| 21 | + init (serverUrl, token) { |
| 22 | + if (this.authenticated) { |
| 23 | + console.warn('[RealTimeClient] WS connection already authenticated.') |
| 24 | + return |
| 25 | + } |
| 26 | + console.log('[RealTimeClient] Initializing') |
| 27 | + this.serverUrl = serverUrl |
| 28 | + this.token = token |
| 29 | + this.connect() |
| 30 | + } |
| 31 | + logout () { |
| 32 | + console.log('[RealTimeClient] Logging out') |
| 33 | + this.subscribeQueue = {} |
| 34 | + this.unsubscribeQueue = {} |
| 35 | + this.authenticated = false |
| 36 | + this.loggedOut = true |
| 37 | + this.socket && this.socket.close() |
| 38 | + } |
| 39 | + connect () { |
| 40 | + console.log('[RealTimeClient] Connecting to ' + this.serverUrl) |
| 41 | + this.socket = new SockJS(this.serverUrl + '?token=' + this.token) |
| 42 | + this.socket.onopen = () => { |
| 43 | + // Once the connection established, always set the client as authenticated |
| 44 | + this.authenticated = true |
| 45 | + this._onConnected() |
| 46 | + } |
| 47 | + this.socket.onmessage = (event) => { |
| 48 | + this._onMessageReceived(event) |
| 49 | + } |
| 50 | + this.socket.onerror = (error) => { |
| 51 | + this._onSocketError(error) |
| 52 | + } |
| 53 | + this.socket.onclose = (event) => { |
| 54 | + this._onClosed(event) |
| 55 | + } |
| 56 | + } |
| 57 | + subscribe (channel, handler) { |
| 58 | + if (!this._isConnected()) { |
| 59 | + this._addToSubscribeQueue(channel, handler) |
| 60 | + return |
| 61 | + } |
| 62 | + const message = { |
| 63 | + action: 'subscribe', |
| 64 | + channel |
| 65 | + } |
| 66 | + this._send(message) |
| 67 | + this.$bus.$on(this._channelEvent(channel), handler) |
| 68 | + console.log('[RealTimeClient] Subscribed to channel ' + channel) |
| 69 | + } |
| 70 | + unsubscribe (channel, handler) { |
| 71 | + // Already logged out, no need to unsubscribe |
| 72 | + if (this.loggedOut) { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + if (!this._isConnected()) { |
| 77 | + this._addToUnsubscribeQueue(channel, handler) |
| 78 | + return |
| 79 | + } |
| 80 | + const message = { |
| 81 | + action: 'unsubscribe', |
| 82 | + channel |
| 83 | + } |
| 84 | + this._send(message) |
| 85 | + this.$bus.$off(this._channelEvent(channel), handler) |
| 86 | + console.log('[RealTimeClient] Unsubscribed from channel ' + channel) |
| 87 | + } |
| 88 | + _isConnected () { |
| 89 | + return this.socket && this.socket.readyState === SockJS.OPEN |
| 90 | + } |
| 91 | + _onConnected () { |
| 92 | + globalBus.$emit('RealTimeClient.connected') |
| 93 | + console.log('[RealTimeClient] Connected') |
| 94 | + |
| 95 | + // Handle subscribe and unsubscribe queue |
| 96 | + this._processQueues() |
| 97 | + } |
| 98 | + _onMessageReceived (event) { |
| 99 | + const message = JSON.parse(event.data) |
| 100 | + console.log('[RealTimeClient] Received message', message) |
| 101 | + |
| 102 | + if (message.channel) { |
| 103 | + this.$bus.$emit(this._channelEvent(message.channel), message.payload) |
| 104 | + } |
| 105 | + } |
| 106 | + _send (message) { |
| 107 | + this.socket.send(JSON.stringify(message)) |
| 108 | + } |
| 109 | + _onSocketError (error) { |
| 110 | + console.error('[RealTimeClient] Socket error', error) |
| 111 | + } |
| 112 | + _onClosed (event) { |
| 113 | + console.log('[RealTimeClient] Received close event', event) |
| 114 | + if (this.loggedOut) { |
| 115 | + // Manually logged out, no need to reconnect |
| 116 | + console.log('[RealTimeClient] Logged out') |
| 117 | + globalBus.$emit('RealTimeClient.loggedOut') |
| 118 | + } else { |
| 119 | + // Temporarily disconnected, attempt reconnect |
| 120 | + console.log('[RealTimeClient] Disconnected') |
| 121 | + globalBus.$emit('RealTimeClient.disconnected') |
| 122 | + |
| 123 | + setTimeout(() => { |
| 124 | + console.log('[RealTimeClient] Reconnecting') |
| 125 | + globalBus.$emit('RealTimeClient.reconnecting') |
| 126 | + this.connect() |
| 127 | + }, 1000) |
| 128 | + } |
| 129 | + } |
| 130 | + _channelEvent (channel) { |
| 131 | + return 'channel:' + channel |
| 132 | + } |
| 133 | + _processQueues () { |
| 134 | + console.log('[RealTimeClient] Processing subscribe/unsubscribe queues') |
| 135 | + |
| 136 | + // Process subscribe queue |
| 137 | + const subscribeChannels = Object.keys(this.subscribeQueue) |
| 138 | + subscribeChannels.forEach(channel => { |
| 139 | + const handlers = this.subscribeQueue[channel] |
| 140 | + handlers.forEach(handler => { |
| 141 | + this.subscribe(channel, handler) |
| 142 | + this._removeFromQueue(this.subscribeQueue, channel, handler) |
| 143 | + }) |
| 144 | + }) |
| 145 | + |
| 146 | + // Process unsubscribe queue |
| 147 | + const unsubscribeChannels = Object.keys(this.unsubscribeQueue) |
| 148 | + unsubscribeChannels.forEach(channel => { |
| 149 | + const handlers = this.unsubscribeQueue[channel] |
| 150 | + handlers.forEach(handler => { |
| 151 | + this.unsubscribe(channel, handler) |
| 152 | + this._removeFromQueue(this.unsubscribeQueue, channel, handler) |
| 153 | + }) |
| 154 | + }) |
| 155 | + } |
| 156 | + _addToSubscribeQueue (channel, handler) { |
| 157 | + console.log('[RealTimeClient] Adding channel subscribe to queue. Channel: ' + channel) |
| 158 | + // To make sure the unsubscribe won't be sent out to the server |
| 159 | + this._removeFromQueue(this.unsubscribeQueue, channel, handler) |
| 160 | + const handlers = this.subscribeQueue[channel] |
| 161 | + if (!handlers) { |
| 162 | + this.subscribeQueue[channel] = [handler] |
| 163 | + } else { |
| 164 | + handlers.push(handler) |
| 165 | + } |
| 166 | + } |
| 167 | + _addToUnsubscribeQueue (channel, handler) { |
| 168 | + console.log('[RealTimeClient] Adding channel unsubscribe to queue. Channel: ' + channel) |
| 169 | + // To make sure the subscribe won't be sent out to the server |
| 170 | + this._removeFromQueue(this.subscribeQueue, channel, handler) |
| 171 | + const handlers = this.unsubscribeQueue[channel] |
| 172 | + if (!handlers) { |
| 173 | + this.unsubscribeQueue[channel] = [handler] |
| 174 | + } else { |
| 175 | + handlers.push(handlers) |
| 176 | + } |
| 177 | + } |
| 178 | + _removeFromQueue (queue, channel, handler) { |
| 179 | + const handlers = queue[channel] |
| 180 | + if (handlers) { |
| 181 | + let index = handlers.indexOf(handler) |
| 182 | + if (index > -1) { |
| 183 | + handlers.splice(index, 1) |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +export default new RealTimeClient() |
0 commit comments