【发布时间】:2018-10-19 19:41:21
【问题描述】:
在 Javascript 中使用 stomp.js 和 sockjs 时,我可以很好地与 Spring Boot 后端连接。
在 Angular5 中使用 stompjs 和 sockjs 时,我不断收到以下错误:
InvalidStateError: 连接尚未建立
有解决方法吗?只是添加 sockjs.min.js,as mentioned in this post,并没有帮助。
详细日志为:
设置连接/1 main.3388a5e3a20e64e3bdb8.bundle.js:1 设置 up connection/2 main.3388a5e3a20e64e3bdb8.bundle.js:1 去 订阅 ... main.3388a5e3a20e64e3bdb8.bundle.js:1 打开网页 套接字... main.3388a5e3a20e64e3bdb8.bundle.js:1 >>> 发送 目的地:/app/chat.addUser 内容长度:29
{"sender":"me","type":"JOIN"} main.3388a5e3a20e64e3bdb8.bundle.js:1 ERROR 错误:未捕获(在承诺中):错误:InvalidStateError: 连接尚未建立错误:InvalidStateError: 连接尚未建立 在 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'})
)
【问题讨论】:
标签: javascript angular websocket stomp sockjs