springboot下使用websocket的案例

环境:idea,maven,springboot
首先需要三个js文件:jquery-3.3.1.min.js、sockjs.min.js、stomp.min.js
项目结构:
springboot下使用websocket的案例_第1张图片
需要的jar包有

 
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-websocket
        
        
            org.freemarker
            freemarker
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    

application.properties中的配置文件

  spring.freemarker.suffix=.html

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;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * 配置WebSocket
 */
@Configuration
//注解开启使用STOMP协议来传输基于代理(message broker)的消息,这时控制器支持使用@MessageMapping,就像使用@RequestMapping一样
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Override
    //注册STOMP协议的节点(endpoint),并映射指定的url
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //注册一个STOMP的endpoint,并指定使用SockJS协议
        registry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS();
    }

    @Override
    //配置消息代理(Message Broker)
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //点对点应配置一个/user消息代理,广播式应配置一个/topic消息代理
        registry.enableSimpleBroker("/topic", "/user");
        //点对点使用的订阅前缀(客户端订阅路径上会体现出来),不设置的话,默认也是/user/
//        registry.setUserDestinationPrefix("/user");
    }

MyJob中的内容:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;

/**
 * @Description: java类作用描述
 * @Author: chenXingNan
 * @CreateDate: 2019/8/22$ 0:52$
 * @Version: 1.0
 */
@Configuration
public class MyJob {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    @Scheduled(fixedRate = 1000)
    public void sendMessage(){
        System.out.println("来着服务端的消息");
        simpMessagingTemplate.convertAndSend("/topic/getResponse", System.currentTimeMillis());
    }
    @Scheduled(fixedRate = 1000)
    public void sendMessageToUser(){
        System.out.println("来着服务端给指定用户的消息");
        simpMessagingTemplate.convertAndSendToUser("1","/getResponse", LocalDateTime.now());
    }
}

IndexController中的内容:

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Description: java类作用描述
 * @Author: chenXingNan
 * @CreateDate: 2019/8/22$ 0:24$
 * @Version: 1.0
 */
@Controller
@RequestMapping
public class IndexController {
    @GetMapping("/")
    public String index(){
        return "index";
    }
    @GetMapping("/user")
    public String user(long id, ModelMap modelMap){
        modelMap.addAttribute("id",id);
        return "user";
    }
}

index.html中的内容




    socket测试
    
    
    
    


    

user.html中的内容

 


    socket测试
    
    
    
    


只有id为1的人能够收到消息
    

你可能感兴趣的:(springboot下使用websocket的案例)