004.Spring Boot WebSocket Sample

一、目录结构

004.Spring Boot WebSocket Sample_第1张图片
目录结构

二、pom.xml



    4.0.0

    com.airkisser
    spring-boot-sample-websocket
    0.0.1-SNAPSHOT
    war

    spring-boot-sample-websocket
    Spring boot websocket sample

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-websocket
        

        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


三、application.properties

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.content-type=text/html

# 开启 thymeleaf 热部署
spring.thymeleaf.cache=false

四、java

Application.java

package com.airkisser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

WebSocketConfig.java

package com.airkisser.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
// 该注解开启使用STOMP协议来传输基于代理(message broker)的消息,
// 此时控制器支持使用@MessageMapping
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    @Override// 注册STOMP协议的节点(endpoint),并映射指定的URL
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册一个STOMP的endpoint,并指定使用SockJS协议
        registry.addEndpoint("endpointAir").withSockJS();
    }

    @Override// 配置消息代理(Message Broker)
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 配置一个广播式消息代理:"/topic"
        registry.enableSimpleBroker("/topic");
    }
}

WebMvcConfig.java

package com.airkisser.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("welcome");
    }

}

Message.java

package com.airkisser.entity;

import java.io.Serializable;

public class Message implements Serializable {
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

Response.java

package com.airkisser.entity;

import java.io.Serializable;

public class Response implements Serializable {
    private String message;

    public Response(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

WebSocketController.java

package com.airkisser.web;

import com.airkisser.entity.Message;
import com.airkisser.entity.Response;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

@Controller
public class WebSocketController {

    @MessageMapping("/welcome")
    @SendTo("/topic/getResponse")
    public Response welcome(Message message) {
        return new Response("Message: " + message.getMessage());
    }

}

五、welcome.html




    
    Spring Boot WebSocket广播式


六、结果

004.Spring Boot WebSocket Sample_第2张图片
RUN

你可能感兴趣的:(004.Spring Boot WebSocket Sample)