2017-09-06 11 views
1

私のAngular 2プロジェクトでは、ng2-stomp-serviceを使用してサーバーとのソケット接続を確立しています。加入前にSTOMPソケット接続が確立されていることを確認

時間、それが正常に動作しますが、時々私は私の接続がまだ確立されていないことを言って、ソケットに加入しようとすると、このエラーを取得するの大半:

Error: Uncaught (in promise): Error: InvalidStateError: The connection has not been established yet Error: InvalidStateError: The connection has not been established yet 
    at SockJS.send (main.js:158) 
    at Client._transmit (stomp.js:159) 
    at Client.subscribe (stomp.js:379) 
    at StompService.subscribe (stomp.service.ts:132) 
    at slide-manager.service.ts:129 
    at ZoneDelegate.invoke (zone.js:391) 
    at Object.onInvoke (core.es5.js:3933) 
    at ZoneDelegate.invoke (zone.js:390) 
    at Zone.run (zone.js:141) 
    at zone.js:818 
    at SockJS.send (main.js:158) 
    at Client._transmit (stomp.js:159) 
    at Client.subscribe (stomp.js:379) 
    at StompService.subscribe (stomp.service.ts:132) 
    at slide-manager.service.ts:129 
    at ZoneDelegate.invoke (zone.js:391) 
    at Object.onInvoke (core.es5.js:3933) 
    at ZoneDelegate.invoke (zone.js:390) 
    at Zone.run (zone.js:141) 
    at zone.js:818 
    at resolvePromise (zone.js:770) 
    at zone.js:821 
    at ZoneDelegate.invokeTask (zone.js:424) 
    at Object.onInvokeTask (core.es5.js:3924) 
    at ZoneDelegate.invokeTask (zone.js:423) 
    at Zone.runTask (zone.js:191) 
    at drainMicroTaskQueue (zone.js:584) 
    at WebSocket.ZoneTask.invoke (zone.js:490) 

このスタックトレースの一片こと私に属しているのはslide-manager.service.ts:129です。このコードは、このコードのstomp.subscribeラインである:

this.socketConfig = { 
     host: 'http://' + getHost() + '/app-ws', 
     debug: true, 
     queue: {'init': false} 
    }; 

this.stomp.configure(this.socketConfig); 
this.stomp.startConnect().then((frame) => { 
this.stomp.done('init'); 
this.connected = true; 
this.spectraSubscription = this.stomp.subscribe('/topic/spectra', (spectra) => { 
    if (spectra && (!this.theChart || hostElement.childElementCount === 0) && !this.refreshFlag) { 
     this.makeChart(spectra.points, hostElement); 
     this.refreshFlag = true; 
    } else if (spectra && (this.theChart || hostElement.childElementCount > 0) || this.refreshFlag) { 
     this.theChart.collectionView.sourceCollection = spectra.points; 
     this.theChart.collectionView.refresh(); 
    } 
    }); 

どのように私はそれが().subscribeを実行しようとする前に、接続が実際に確立されていることを確認することができますか?私はそれを.then()ブロックに入れることは、接続の約束が解決された後にのみ実行されるべきであるので、トリックを行いますが、接続が存在しなくても(エラーに応じて)引き続きトリガーされているようです。

ここに遊びに欠けていることがありますか?

答えて

1

Iは、内蔵のストンプクライアントの「状態」プロパティをチェックすることで、この問題を解決することができた:

if (this.stomp.status === 'CONNECTED') { 
    // do stuff that requires a connection, like establish subscriptions 
} 
関連する問題