WebSocket的使用流程:1、引入spring-boot-starter-websocket依赖;2、配置类里面@Bean一个ServerEndpointExporter对象;3、编写一个websocket类,标注@Compnent和@ServerEndpoint("url")注解,按需重写onopen,onmessage,onclose方法,session.getBasicRemote().sendText(".....")像浏览器发送响应。
@Scheduled(cron = "*/5 * * * * *")注解是spring内置的,需要在启动类上@EnableScheduling。
onopen方法中存入到sessionHashMap中一个session,后面根据不同浏览器发送的不同voteId可以取出来自己的session。
定时任务中循环上面的 类中的sessionHashMap,完成前端页面的数据 "实时" 推送