2017-11-11 314 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", ...);