|
| 1 | +import { Stomp } from 'stompjs/lib/stomp' |
| 2 | +import SockJS from 'sockjs-client' |
| 3 | +import getBackoff from 'utils/getBackoff' |
| 4 | +import config from 'config' |
| 5 | +import { autorun, runInAction } from 'mobx' |
| 6 | + |
| 7 | +const log = console.log || (x => x) |
| 8 | +const warn = console.warn || (x => x) |
| 9 | + |
| 10 | +const io = require(__RUN_MODE__ ? 'socket.io-client/dist/socket.io.min.js' : 'socket.io-client-legacy/dist/socket.io.min.js') |
| 11 | + |
| 12 | +class FsSocketClient { |
| 13 | + constructor () { |
| 14 | + if (FsSocketClient.$$singleton) return FsSocketClient.$$singleton |
| 15 | + const url = config.isPlatform ? |
| 16 | + `${config.wsURL}/sockjs/${config.spaceKey}` |
| 17 | + : `${config.baseURL}/sockjs/` |
| 18 | + // SockJS auto connects at initiation |
| 19 | + this.sockJSConfigs = [url, {}, { server: `${config.spaceKey}`, transports: 'websocket' }] |
| 20 | + this.backoff = getBackoff({ |
| 21 | + delayMin: 50, |
| 22 | + delayMax: 5000, |
| 23 | + }) |
| 24 | + this.maxAttempts = 7 |
| 25 | + FsSocketClient.$$singleton = this |
| 26 | + } |
| 27 | + |
| 28 | + connect (connectCallback, errorCallback) { |
| 29 | + const self = this |
| 30 | + if (!this.socket || !this.stompClient) { |
| 31 | + this.socket = new SockJS(...this.sockJSConfigs) |
| 32 | + this.stompClient = Stomp.over(this.socket) |
| 33 | + this.stompClient.debug = false // stop logging PING/PONG |
| 34 | + } |
| 35 | + self.stompClient.connect({}, function success () { |
| 36 | + runInAction(() => config.fsSocketConnected = true) |
| 37 | + self.backoff.reset() |
| 38 | + connectCallback.call(this) |
| 39 | + }, function error () { |
| 40 | + log('fsSocket error') |
| 41 | + switch (self.socket.readyState) { |
| 42 | + case SockJS.CLOSING: |
| 43 | + case SockJS.CLOSED: |
| 44 | + runInAction(() => config.fsSocketConnected = false) |
| 45 | + self.reconnect(connectCallback, errorCallback) |
| 46 | + break |
| 47 | + case SockJS.OPEN: |
| 48 | + log('FRAME ERROR', arguments[0]) |
| 49 | + break |
| 50 | + default: |
| 51 | + } |
| 52 | + errorCallback(arguments) |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + reconnect (connectCallback, errorCallback) { |
| 57 | + if (config.fsSocketConnected) return |
| 58 | + log(`try reconnect fsSocket ${this.backoff.attempts}`) |
| 59 | + // unset this.socket |
| 60 | + this.socket = undefined |
| 61 | + if (this.backoff.attempts <= this.maxAttempts) { |
| 62 | + const retryDelay = this.backoff.duration() |
| 63 | + log(`Retry after ${retryDelay}ms`) |
| 64 | + const timer = setTimeout( |
| 65 | + this.connect.bind(this, connectCallback, errorCallback) |
| 66 | + , retryDelay) |
| 67 | + } else { |
| 68 | + this.backoff.reset() |
| 69 | + warn('Sock connected failed, something may be broken, reload page and try again') |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +class TtySocketClient { |
| 76 | + constructor () { |
| 77 | + if (TtySocketClient.$$singleton) return TtySocketClient.$$singleton |
| 78 | + if (config.isPlatform) { |
| 79 | + const wsUrl = config.wsURL |
| 80 | + const firstSlashIdx = wsUrl.indexOf('/', 8) |
| 81 | + let [host, path] = firstSlashIdx === -1 ? [wsUrl, ''] : [wsUrl.substring(0, firstSlashIdx), wsUrl.substring(firstSlashIdx)] |
| 82 | + this.socket = io.connect(host, { |
| 83 | + forceNew: true, |
| 84 | + reconnection: false, |
| 85 | + autoConnect: false, // <- will manually handle all connect/reconnect behavior |
| 86 | + reconnectionDelay: 1500, |
| 87 | + reconnectionDelayMax: 10000, |
| 88 | + reconnectionAttempts: 5, |
| 89 | + path: `${path}/tty/${config.shardingGroup}/${config.spaceKey}/connect`, |
| 90 | + transports: ['websocket'] |
| 91 | + }) |
| 92 | + } else { |
| 93 | + this.socket = io.connect(config.baseURL, { 'resource': 'coding-ide-tty1' }) |
| 94 | + } |
| 95 | + |
| 96 | + this.backoff = getBackoff({ |
| 97 | + delayMin: 1500, |
| 98 | + delayMax: 10000, |
| 99 | + }) |
| 100 | + this.maxAttempts = 5 |
| 101 | + |
| 102 | + TtySocketClient.$$singleton = this |
| 103 | + return this |
| 104 | + } |
| 105 | + |
| 106 | + // manually handle all connect/reconnect behavior |
| 107 | + connectingPromise = undefined |
| 108 | + connect () { |
| 109 | + // Need to make sure EVERY ATTEMPT to connect has ensured `fsSocketConnected == true` |
| 110 | + if (this.socket.connected || this.connectingPromise) return this.connectingPromise |
| 111 | + let resolve, reject |
| 112 | + this.connectingPromise = new Promise((rsv, rjt) => { resolve = rsv; reject = rjt }) |
| 113 | + const dispose = autorun(() => { if (config.fsSocketConnected) resolve(true) }) |
| 114 | + this.connectingPromise.then(() => { |
| 115 | + dispose() |
| 116 | + this.connectingPromise = undefined |
| 117 | + |
| 118 | + // below is the actual working part of `connect()` method, |
| 119 | + // all logic above is just for ensuring `fsSocketConnected == true` |
| 120 | + this.socket.io.connect(err => { |
| 121 | + if (err) { |
| 122 | + runInAction(() => config.ttySocketConnected = false) |
| 123 | + return this.reconnect() |
| 124 | + } |
| 125 | + // success! |
| 126 | + runInAction(() => config.ttySocketConnected = true) |
| 127 | + this.backoff.reset() |
| 128 | + }) |
| 129 | + this.socket.connect() |
| 130 | + }) |
| 131 | + } |
| 132 | + |
| 133 | + reconnect () { |
| 134 | + log(`try reconnect ttySocket ${this.backoff.attempts}`) |
| 135 | + if (this.backoff.attempts <= this.maxAttempts && !this.socket.connected) { |
| 136 | + const timer = setTimeout(() => { |
| 137 | + this.connect() |
| 138 | + }, this.backoff.duration()) |
| 139 | + } else { |
| 140 | + warn(`TTY reconnection fail after ${this.backoff.attempts} attempts`) |
| 141 | + this.backoff.reset() |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | +} |
| 146 | + |
| 147 | +export { FsSocketClient, TtySocketClient } |
0 commit comments