【问题标题】:JavaScript client cannot connect to Spring 4 WebSocketJavaScript 客户端无法连接到 Spring 4 WebSocket
【发布时间】:2017-02-02 12:27:42
【问题描述】:

我已经在spring中实现了websocket,但是JavaScript客户端无法连接到websocket。

这里是 WebSocketConfig 类:

package com.myapp.spring.security.config;
import com.myapp.spring.web.controller.MyWebSocketHandler;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.PerConnectionWebSocketHandler;

@Configuration
@EnableWebMvc
@EnableWebSocket
@ComponentScan(basePackages={"com.myapp.spring.*"})
public class WebSocketConfig implements WebSocketConfigurer {

    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry
                .addHandler(myWebSocketHandler(), "/endpoint")
                .setAllowedOrigins("*");
    }

    @Bean
    public WebSocketHandler myWebSocketHandler() {
        return new PerConnectionWebSocketHandler(MyWebSocketHandler.class);
    }


}

这是尝试连接到 websocket 的 test.html 页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

      <body>

        <p>Going to connect to the WebSocket at /endpoint. Check Console</p>
        <button onclick="ss()">Send</button>
      </body>

      <script type="text/javascript">
        var webSocket = new WebSocket("wss://"+document.location.hostname+":8443"+"/endpoint");
        webSocket.onopen = function(message) {
                processOpen(message);
            };
            webSocket.onmessage = function(message) {
                processMessage(message);
            };
            webSocket.onclose = function(message) {
                processClose(message);
            };
            webSocket.onerror = function(message) {
                processError(message);
            };


            function processOpen(message) {
                console.log("JS: Server Connected... "+message);
            }
            function processMessage(message) {
                console.log("Getting a mess: "+message);
            }
            function processClose(message) {
                console.log("JS: Client disconnected... "+message);
            }
            function processError(message) { //
                console.log("Error occured: "+message);
            }

            function ss() {
                webSocket.send("test");
            }
      </script>

</html>

我将 websocket 路径初始化为/endpoint。这从我的服务器日志中可以明显看出这已经发生了:

[org.springframework.web.socket.server.support.WebSocketHandlerMapping] (ServerService Thread Pool -- 75) Mapped URL path [/endpoint] onto handler of type [class org.springframework.web.socket.server.support.WebSocketHttpRequestHandler]

当我打开test.html 时,连接打开并立即断开连接。 processOpen(message)processClose(message) 函数被立即调用,一个接一个。那么我做错了什么,我该如何解决这个问题?

【问题讨论】:

    标签: javascript spring spring-mvc websocket spring-websocket


    【解决方案1】:

    test.html 中的 java 脚本代码看起来不错。关闭连接的 Spring Web 套接字配置可能存在一些问题。以下是带有 spring boot 的 web socket 的工作代码。请与您的配置进行比较。

    pom.xml 中的 Web 套接字依赖项

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    

    WebSocketHandler 类

    @Component
    public class EchoHandler extends TextWebSocketHandler {
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        TextMessage echoMessage = new TextMessage("Echo :" + message.getPayload());
        System.out.println("Sending "+echoMessage.getPayload());
        session.sendMessage(echoMessage);
      }
    }
    

    WebSocket 控制器类

    @EnableWebSocket
    @Controller
    public class WSController implements WebSocketConfigurer {
    @Autowired
    private EchoHandler echoHandler;
    
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(echoHandler, "/echo").setAllowedOrigins("*"); 
       }
    }
    

    Spring Boot 应用类

    @SpringBootApplication
    public class WSApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(WSApplication.class, args);
      }
    }
    

    【讨论】:

    • 这不起作用。我在 WebSocketConfig 中收到一个错误(说明需要 bean)
    • 您能否显示确切的错误消息和您更新的课程?我发布的代码对我来说很好。
    • 我绝对可以并且愿意。但是我在发布这个问题后意识到我关于网络套接字的问题略有不同。我又发了一个 SO 帖子:stackoverflow.com/questions/39683062/… 如果你能帮忙,那就太棒了。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2017-01-25
    • 2020-08-17
    • 2017-11-30
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    相关资源
    最近更新 更多