gpt4 book ai didi

javascript - Websocket-sockjs-InvalidStateError : The connection has not been established yet

转载 作者:行者123 更新时间:2023-11-28 17:18:44 51 4
gpt4 key购买 nike

当在 Javascript 中使用 stomp.js 和 sockjs 时,我可以与 Spring Boot 后端很好地连接。

当在 Angular5 中使用 stompjs 和 sockjs 时,我不断收到这些错误:

InvalidStateError:连接尚未建立

有解决办法吗?只需添加 sockjs.min.js,as mentioned in this post ,没有帮助。

详细日志为:

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

{"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)

我的 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'})
)

最佳答案

现在,在启动(!)异步获取连接调用后直接调用发送方法。那是错误的。

有两种解决方案:

  • 好:将发送调用放在获取连接的异步主体中。所以连接建立后,调用send方法。
  • 不是最佳选择:WAITING一段时间才能完成异步获取连接。

关于javascript - Websocket-sockjs-InvalidStateError : The connection has not been established yet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52898952/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com