Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用HTML5WebSocket API创建SpringWebSocket应用程序?_Spring_Spring Mvc_Spring Boot_Spring Websocket - Fatal编程技术网

如何使用HTML5WebSocket API创建SpringWebSocket应用程序?

如何使用HTML5WebSocket API创建SpringWebSocket应用程序?,spring,spring-mvc,spring-boot,spring-websocket,Spring,Spring Mvc,Spring Boot,Spring Websocket,最新版本的SpringWebSocket与SockJS和StompJS库一起使用但我不喜欢在我的应用程序中使用主题。因此,如何使用创建SpringWebSocket应用程序并将我们的应用程序与SpringSecurity集成?我找不到任何关于如何在没有sockjs的情况下配置SpringWebSocket的好例子,但我在Spring文档站点中发现了一些有用的例子,我很乐意与大家分享那么,如何使用HTML5 WebSocket API创建Spring WebSocket应用程序? 首先:创建一个类

最新版本的SpringWebSocketSockJSStompJS库一起使用但我不喜欢在我的应用程序中使用主题。因此,如何使用创建SpringWebSocket应用程序并将我们的应用程序与SpringSecurity集成?

我找不到任何关于如何在没有sockjs的情况下配置SpringWebSocket的好例子,但我在Spring文档站点中发现了一些有用的例子,我很乐意与大家分享那么,如何使用HTML5 WebSocket API创建Spring WebSocket应用程序?

首先:创建一个类,该类扩展TextWebSocketHandlerBinaryWebSocketHandler,并使用@组件注释对其进行注释,并覆盖其相应的方法。该类的工作方式类似于控制器

@Component
public class SimpleWebSocketHandler extends TextWebSocketHandler {
    @Override
    protected void handleTextMessage(WebSocketSession session, 
            TextMessage message) throws Exception {
        // Sends back response to client.
        session.sendMessage(new TextMessage("Connection is all right."));
    }
}
第二步:创建一个配置类,该类实现WebSocketConfigurer,并用@Configuration@EnableWebSocket注释对其进行注释,并覆盖其相应的方法。该类使用处理程序类我们已经创造了

@Configuration
@EnableWebSocket
public class WebSocketConfigurations implements WebSocketConfigurer {
    @Autowired
    private SimpleWebSocketHandler simpleWebSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // Regsiters a handler for related endpoint.
        registry.addHandler(simpleWebSocketHandler, "/chat");
    }
}
第三:将所有WebSokcet端点添加到Spring安全配置中

httpSecurity.authorizeRequests()
    .antMatchers("/chat").permitAll();
// Create WebSocket Object.
var ws = new WebSocket("ws://localhost:8080/chat");

// Runs when connecion is estabilished.
ws.onopen = function () {
    // Sends request to server with string value.
    ws.send("webSocket");
};

// Runs when response is ready.
// Use event to get response value.
ws.onmessage = function (event) {

};
第四:我们使用适当的URL创建一个新的javascript WebSocket对象

httpSecurity.authorizeRequests()
    .antMatchers("/chat").permitAll();
// Create WebSocket Object.
var ws = new WebSocket("ws://localhost:8080/chat");

// Runs when connecion is estabilished.
ws.onopen = function () {
    // Sends request to server with string value.
    ws.send("webSocket");
};

// Runs when response is ready.
// Use event to get response value.
ws.onmessage = function (event) {

};

注意:WebSocket URL格式:
ws://domain:port/endpoint

谢谢,但我只能在registry.addHandler(simpleWebSocketHandler,“/chat”).setAllowedOrigins(“*”)之后使用spring WebSocket;否则我会得到302或403个错误。在我的情况下,Spring安全配置并没有真正的帮助。