简体   繁体   English

Websocket - sockjs - InvalidStateError:连接尚未建立

[英]Websocket - sockjs - InvalidStateError: The connection has not been established yet

When using stomp.js and sockjs from within Javascript, I can connect well with a Spring Boot backend.在 Javascript 中使用 stomp.js 和 sockjs 时,我可以很好地与 Spring Boot 后端连接。

When using stompjs and sockjs from with Angular5, I keep on getting these errors:在 Angular5 中使用 stompjs 和 sockjs 时,我不断收到以下错误:

InvalidStateError: The connection has not been established yet InvalidStateError:连接尚未建立

Is there a workaround?有解决方法吗? Just adding the sockjs.min.js, as mentioned in this post , was not helping.如本文所述,仅添加 sockjs.min.js 并没有帮助。

The detailed log is:详细日志如下:

Setting up connection/1 main.3388a5e3a20e64e3bdb8.bundle.js:1 Setting up connection/2 main.3388a5e3a20e64e3bdb8.bundle.js:1 Going to subscribe ... main.3388a5e3a20e64e3bdb8.bundle.js:1 Opening Web Socket... main.3388a5e3a20e64e3bdb8.bundle.js:1 >>> SEND destination:/app/chat.addUser content-length:29建立连接/1 main.3388a5e3a20e64e3bdb8.bundle.js:1 建立连接/2 main.3388a5e3a20e64e3bdb8.bundle.js:1 去订阅... main.3388a5e3a20e64e3bdb8.bundle.js:1 打开Web Socket... main .3388a5e3a20e64e3bdb8.bundle.js:1 >>> 发送目的地:/app/chat.addUser content-length:29

{"sender":"me","type":"JOIN"} main.3388a5e3a20e64e3bdb8.bundle.js:1 ERROR Error: Uncaught (in promise): Error: InvalidStateError: The connection has not been established yet Error: InvalidStateError: The connection has not been established yet at r.send (scripts.d6f701ecf84f24372966.bundle.js:1) {"sender":"me","type":"JOIN"} main.3388a5e3a20e64e3bdb8.bundle.js:1 错误错误:未捕获(承诺中):错误:InvalidStateError:尚未建立连接错误:InvalidStateError:在 r.send (scripts.d6f701ecf84f24372966.bundle.js:1) 处尚未建立连接

My code in Angular (translated from Javascript) is:我在 Angular 中的代码(从 Javascript 翻译)是:

 let ws = new SockJS(this.serverUrl);
    this.stompClient = Stomp.over(ws);
    let that = this;
    console.log('Setting up connection/2');
    console.log('Going to subscribe ... ');
    this.stompClient.connect({}, function (frame) {
      console.log('Going to subscribe ... ');
      that.stompClient.subscribe('/topic/public', (payload) => {
          console.log('Subscribe: Incoming message: ' + payload.body);
          if (payload.body) {
            let message = JSON.parse(payload.body);
            if (message.sender === 'MyBot') {
              this.createAndAddChat('you', message.content);
            } else {
              this.createAndAddChat('me', message.content);
            }
            console.log('New message arrived: ' + payload.body);
          }
        },
        error => {
          console.log( 'Subscribe: error: ' + error)
        },
        () => {
         console.log( 'Subscribe, On complete')
        });
    });
    this.stompClient.send("/app/chat.addUser", {},
      JSON.stringify({sender: 'me', type: 'JOIN'})
    )

The send method is now called directly after starting (!) the asynchronous get-connection call.现在在启动(!)异步 get-connection 调用之后直接调用 send 方法。 That's wrong.那是错误的。

There are 2 solutions:有2个解决方案:

  • Good: Put the send call within the async-body of the get-connection.好:将 send 调用放在 get-connection 的 async-body 中。 So after the connection is made, call the send method.所以在建立连接后,调用 send 方法。
  • Not optimal: Wait for some time to have the async get-connection to complete.不是最佳的:等待一段时间以完成异步获取连接。
    stompClient.connect({}, function (frame) {
        console.log('Connected: ' + frame);
        setTimeout(function() {
           stompClient.subscribe("/topic/mytopic/", function (message) {
              showDeviceConnection(deviceIds[i], message.body);
           });
        }, 500);});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM