2017-11-11 313 views
1

我遵循this tutorial構建了一個Spring Boot應用程序,該應用程序爲websockets連接提供服務,但是我無法連接到除Spring Boot本身提供的其他客戶端的這些WebSocket。CORS和Spring Websocket

complete directory in the GitHub repository that accompanies the tutorial包含最終的Spring啓動代碼。我從此存儲庫獲取了index.htmlapp.js文件,並創建了另一個在Node.js服務器上運行的客戶端。之後,我將連接字符串替換爲指向localhost:8080(其中Spring Boot正在運行)。然後我運行Node.js服務器並試圖使用websockets,但它不起作用。

第一個問題很容易通過將.setAllowedOrigins("*")添加到StompEndpointRegistry註冊表中解決。有了這個配置,我設法連接到websocket,但是現在我永遠不會從套接字中獲取消息。

我不知道我錯過了什麼......有人知道什麼是問題嗎?

提取的index.htmlapp.js(更名爲index.js)文件,以及the Node.js server can be found here用於測試目的。要運行它,只需安裝依賴項(npm install),然後發出npm start。服務器將在http://localhost:3000/上進行響應。

回答

0

其實問題很愚蠢。問題是,當我提取的HTML/JS文件到外部應用程序,我改變了所有硬編碼的三個點在代碼中http://localhost:8080/...

var socket = new SockJS('http://localhost:8080/gs-guide-websocket'); 
// ... 
stompClient.subscribe('http://localhost:8080/topic/greetings', cb); 
// ... 
stompClient.send("http://localhost:8080/app/hello", ...); 

,我應該改變的唯一的線是第一個。另外兩個只是subscribesend消息到已打開的套接字上的主題的功能。因此,他們不需要URL作爲前綴...

var socket = new SockJS('http://localhost:8080/gs-guide-websocket'); 
// ... 
stompClient.subscribe('/topic/greetings', cb); 
// ... 
stompClient.send("/app/hello", ...);