@@ -5,6 +5,7 @@ import emitter, * as E from 'utils/emitter'
55import config from 'config'
66import { autorun , runInAction } from 'mobx'
77import { notify , NOTIFY_TYPE } from '../components/Notification/actions'
8+ import * as searchAPI from 'backendAPI/searchAPI'
89
910const log = console . log || ( x => x )
1011const warn = console . warn || ( x => x )
@@ -93,6 +94,9 @@ class FsSocketClient {
9394 if ( TtySocketClient . $$singleton ) {
9495 TtySocketClient . $$singleton . close ( ) ;
9596 }
97+ if ( SearchSocketClient . $$singleton ) {
98+ SearchSocketClient . $$singleton . close ( ) ;
99+ }
96100 }
97101}
98102
@@ -179,4 +183,105 @@ class TtySocketClient {
179183 }
180184}
181185
182- export { FsSocketClient , TtySocketClient }
186+ class SearchSocketClient {
187+ constructor ( ) {
188+ if ( SearchSocketClient . $$singleton ) return SearchSocketClient . $$singleton
189+
190+ const wsUrl = config . wsURL
191+ const firstSlashIdx = wsUrl . indexOf ( '/' , 8 )
192+ const [ host , path ] = firstSlashIdx === - 1 ? [ wsUrl , '' ] : [ wsUrl . substring ( 0 , firstSlashIdx ) , wsUrl . substring ( firstSlashIdx ) ]
193+
194+ // const url = `${host}:8066/search/sockjs`
195+ const url = `${ host } ${ path } /search/sockjs/${ config . spaceKey } `
196+ // http://dev.coding.ide/ide-ws/search/sockjs/kfddvb/info
197+ this . sockJSConfigs = [ url , { } , { server : `${ config . spaceKey } ` , transports : 'websocket' } ]
198+
199+ this . backoff = getBackoff ( {
200+ delayMin : 1500 ,
201+ delayMax : 10000 ,
202+ } )
203+ this . maxAttempts = 5
204+
205+ SearchSocketClient . $$singleton = this
206+ emitter . on ( E . SOCKET_RETRY , ( ) => {
207+ this . reconnect ( )
208+ } )
209+ }
210+
211+ connect ( ) {
212+ if ( ! this . socket || ! this . stompClient ) {
213+ this . socket = new SockJS ( ...this . sockJSConfigs )
214+ this . stompClient = Stomp . over ( this . socket )
215+ this . stompClient . debug = false // stop logging PING/PONG
216+ }
217+ const success = ( ) => {
218+ runInAction ( ( ) => config . searchSocketConnected = true )
219+ this . backoff . reset ( )
220+ this . successCallback ( this . stompClient )
221+ }
222+ const error = ( frame ) => {
223+ if ( this . shouldClose ) {
224+ this . shouldClose = false ;
225+ return ;
226+ }
227+ log ( '[SEARCH Socket] SearchSocket error' , this . socket )
228+ switch ( this . socket . readyState ) {
229+ case SockJS . CLOSING :
230+ case SockJS . CLOSED :
231+ runInAction ( ( ) => config . searchSocketConnected = false )
232+ this . reconnect ( )
233+ break
234+ case SockJS . OPEN :
235+ log ( 'FRAME ERROR' , frame )
236+ break
237+ default :
238+ }
239+ this . errorCallback ( frame )
240+ }
241+
242+ this . stompClient . connect ( { } , success , error )
243+ }
244+
245+ reconnect ( ) {
246+ if ( config . searchSocketConnected ) return
247+ log ( `[SEARCH Socket] reconnect searchSocket ${ this . backoff . attempts } ` )
248+ // unset this.socket
249+ this . socket = undefined
250+ if ( this . backoff . attempts <= this . maxAttempts ) {
251+ const retryDelay = this . backoff . duration ( )
252+ log ( `Retry after ${ retryDelay } ms` )
253+ const timer = setTimeout (
254+ this . connect . bind ( this )
255+ , retryDelay )
256+ } else {
257+ // must emit ,ops correct?
258+ // emitter.emit(E.SOCKET_TRIED_FAILED)
259+ notify ( { message : i18n `global.onSocketError` , notifyType : NOTIFY_TYPE . ERROR } )
260+ this . backoff . reset ( )
261+ warn ( 'Sock connected failed, something may be broken, reload page and try again' )
262+ }
263+ }
264+
265+ close ( ) {
266+ const self = this
267+ if ( config . searchSocketConnected ) {
268+ searchAPI . searchWorkspaceDown ( ) ;
269+ self . shouldClose = true ;
270+ self . socket . close ( ) ;
271+ // must emit ???
272+ // emitter.emit(E.SOCKET_TRIED_FAILED);
273+ runInAction ( ( ) => config . searchSocketConnected = false ) ;
274+ }
275+ }
276+
277+ subscribe = ( topic , process ) => {
278+ this . stompClient . subscribe ( topic , process ) ;
279+ }
280+
281+ send = ( mapping , headers , data ) => {
282+ this . stompClient . send ( mapping , headers , data ) ;
283+ }
284+ }
285+
286+
287+ export { FsSocketClient , TtySocketClient , SearchSocketClient }
0 commit comments