我遵循this tutorial構建了一個Spring Boot應用程序,該應用程序爲websockets
連接提供服務,但是我無法連接到除Spring Boot本身提供的其他客戶端的這些WebSocket。CORS和Spring Websocket
complete
directory in the GitHub repository that accompanies the tutorial包含最終的Spring啓動代碼。我從此存儲庫獲取了index.html
和app.js
文件,並創建了另一個在Node.js服務器上運行的客戶端。之後,我將連接字符串替換爲指向localhost:8080
(其中Spring Boot正在運行)。然後我運行Node.js服務器並試圖使用websockets,但它不起作用。
第一個問題很容易通過將.setAllowedOrigins("*")
添加到StompEndpointRegistry
註冊表中解決。有了這個配置,我設法連接到websocket,但是現在我永遠不會從套接字中獲取消息。
我不知道我錯過了什麼......有人知道什麼是問題嗎?
提取的index.html
和app.js
(更名爲index.js
)文件,以及the Node.js server can be found here用於測試目的。要運行它,只需安裝依賴項(npm install
),然後發出npm start
。服務器將在http://localhost:3000/上進行響應。