angular - 确保在订阅之前建立 STOMP 套接字连接

标签 angular websocket stomp

在我的 Angular 2 项目中,我使用了 ng2-stomp-service建立到服务器的套接字连接。

大多数情况下它工作正常,但有时我在尝试订阅套接字时出现此错误,说我的连接尚未建立:

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() block 中就可以了,因为它应该只在连接的 promise 已经解决后执行,但显然即使不存在连接(根据错误)它仍然会被触发。

还是我遗漏了更多的东西?

最佳答案

我能够通过检查 stomp 客户端的内置“状态”属性来解决这个问题:

if (this.stomp.status === 'CONNECTED') {
    // do stuff that requires a connection, like establish subscriptions
}

关于angular - 确保在订阅之前建立 STOMP 套接字连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46076683/

相关文章:

javascript - 使用 sockjs 和 stomp 在/信息请求期间没有 cookie

angular - MSAL.js protectedResourceMap - 需要解释将其设置为什么

使用 Observable 作为数据源的 Angular 拖放

angular - 可观察的定义是什么意思?

node.js - 单元测试 Node.js 和 WebSockets (Socket.io)

azure - APIM : Limit concurrent open websocket connections

flutter - Websocket、STOMP在flutter应用中的使用

angular - 根据类型渲染动态模板

python - 如何在 Windows 上将 websockify 与 python 一起使用或将 websockify 编译为 Windows 可执行文件(一次用于多个客户端连接)

spring - STOMP over websockets 与普通 STOMP。哪一个更好?