websocket 第二篇

参考文章:https://spring.io/guides/gs/messaging-stomp-websocket/

打不开页面http://localhost:8080/index.html 

1、换一个版本就行了
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
</parent>

2、要放到static文件夹项目

项目结构:

3、websocketconfig 只开启这个基站

//暂时不用另外一个webSocketConfig(第3篇的那个)
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

这个类的关键作用:

public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {



    /**
     *  * enableSimpleBroker 服务端推送给客户端的路径前缀
     * setApplicationDestinationPrefixes  客户端发送数据给服务器端的一个前缀
     *
     * 全过程如下:/app/hello(app.js的send name) → 来到这里的(/app),找到这里的/topic 订阅者,
     *  根据control类     @MessageMapping("/hello")   @SendTo("/topic/greetings")     *
     *  然后转发出去。     *
     * */

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

代码:

https://gitee.com/dgx555/spring.git

发布了149 篇原创文章 · 获赞 8 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/bluewelkin/article/details/102970149
今日推荐