我在前端使用SockJS和STOMP以一种简单的标准方式尝试连接到基于Grails的服务器。前端和后端位于不同的服务器上(我在一个工作团队中,因此无法更改此设置),前端位于端口4200上,后端位于8080上。查看Firefox开发人员工具中的网络连接,看来SockJS正在尝试连接端口4200:
Request URL: http://localhost:4200/sockjs-node/842/cxrvvsec/websocket
当我需要它连接到8080上的后端服务器时,是否可以更改此方法?我在连接代码中指定8080:
private serverUrl = 'http://localhost:8080/stomp'
private sockjsclient = null; // SockJS socket that connects to the server (preferably using a WebSocket)
private stompClient = null; // Stomp client that handles sending messages over the WebSocket
this.sockjsclient = new SockJS(this.serverUrl); // Create a new SockJS socket
this.stompClient = Stomp.over(this.sockjsclient); // Create a STOMP client to send messages over the socket
let that = this;
console.log(this.sockjsclient, this.stompClient)
this.stompClient.connect({}, function() {
console.log('*** subscribeToTopic: CONNECTION ESTABLISHED')
return that.stompClient.subscribe(topic, (message) => callbackfn);
})
浏览器控制台输出:
Opening Web Socket...
Whoops! Lost connection to http://localhost:4200/stomp
谢谢