SpringBoot中的WebSocket点对点发送消息
来源:互联网 发布:windows优化大师固态 编辑:程序博客网 时间:2024/05/18 01:19
添加依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId><version>1.3.5.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>配置SpringSecurity:
package com.example.demo.part3.chapter7.Chat;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.builders.WebSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration@EnableWebSecuritypublic class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() //在内存中分别配置两个用户sasa sa和密码 ,角色是user .withUser("sasa").password("123").roles("user") .and() .withUser("sa").password("123").roles("user"); } @Override public void configure(WebSecurity web) throws Exception { ///resources/static/ 目录下的静态资源,spring security不拦截 web.ignoring().antMatchers("/resources/static/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() //设置spring security对 / 和 /login 路径不拦截 .antMatchers("/","/login").permitAll() .anyRequest().authenticated() .and() .formLogin() //设置spring security的登录页面访问路径为 /login .loginPage("/login") //登陆成功后转向 /chat 路径 .defaultSuccessUrl("/chat") .permitAll() .and() .logout() .permitAll(); }}配置WebSocket:
package com.example.demo.part3.chapter7.Chat;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@EnableWebSocketMessageBrokerpublic class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{ @Override public void configureMessageBroker(MessageBrokerRegistry registry) { //点对点式应增加一个/queue消息代理 registry.enableSimpleBroker("/queue","/topic"); } @Override public void registerStompEndpoints(StompEndpointRegistry endpoint) { //注册一个名为 endPointChat 的endpoint endpoint.addEndpoint("/endPointChat").withSockJS(); }}控制器:
package com.example.demo.part3.chapter7.Chat;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.messaging.handler.annotation.MessageMapping;import org.springframework.messaging.simp.SimpMessagingTemplate;import org.springframework.stereotype.Controller;import java.security.Principal;@Controllerpublic class ChatController { @Autowired //通过simpleMessagingTemplate向浏览器发送消息 private SimpMessagingTemplate messagingTemplate; @MessageMapping("/chat") //在springmvc中,可以直接在参数中获取principle,principle中包含当前用户的信息 public void handleChat(Principal principal,String msg){ System.out.println(1); if(principal.getName().equals("sasa")){ //通过 convertAndSendToUser方法向用户发送信息,第一个参数是消息接收人, //第二个是浏览器订阅地址,第三个是消息本身 System.out.println(2); messagingTemplate.convertAndSendToUser("sa", "/queue/notifications",principal.getName() +"-send:"+msg); }else{ System.out.println(3); messagingTemplate.convertAndSendToUser("sasa", "/queue/notifications",principal.getName() +"-send:"+msg); } }}
登录页面:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><meta charset="UTF-8"/><head> <title>登录页面</title></head><body> <div th:if="${param.error}">无效的账号和密码</div> <div th:if="${param.logout}">你已注销</div> <form th:action="@{/login}" method="post"> <div> <label> 账号:<input type="text" name="username"/> </label> </div> <div> <label> 密码:<input type="password" name="password"/> </label> </div> <div> <input type="submit" name="登录"/> </div> </form></body></html>
聊天页面:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><meta charset="UTF-8"/><head> <title>聊天框</title> <script th:src="@{sockjs.min.js}"></script> <script th:src="@{stomp.min.js}"></script> <script th:src="@{jquery-1.8.3.min.js}"></script></head><body> <form id="chatForm"> <textarea rows="4" cols="60" name="text" id="text" /> <input type="submit"/> </form> <script type="text/javascript"> $("#chatForm").submit(function (e) { e.preventDefault(); var text=$("#text").val(); sendMsg(text); }); //连接endpoint名称为 /endPointChat 的endpoint var sock=new SockJS("/endPointChat"); var stomp=Stomp.over(sock); stomp.connect('guest','guest',function (frame) { //订阅 /user/queue/notifications 发送的消息,这里与 //在控制器的messagingTemplate.convertAndSendToUser中定义的订阅地址保持一致 //这里多了 /user ,并且这个 /user是必须的,使用了 /user 才会将消息发送到指定用户 stomp.subscribe("/user/queue/notifications",handleNotification); }); function handleNotification(message) { console.log("msg"+message); $("#output").append('<b>'+message.body+'</b>'); } function sendMsg(text) { stomp.send("/chat",{},text); } $("#stop").click(function () { sock.close(); }) </script> <div id="output"></div></body></html>页面控制:
package com.example.demo.part3.chapter7.WebSocket;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter{ @Override public void addViewControllers(ViewControllerRegistry registry) { //super.addViewControllers(registry); registry.addViewController("/login").setViewName("/login"); registry.addViewController("/chat").setViewName("/chat"); }}
效果展示:
阅读全文
0 0
- SpringBoot中的WebSocket点对点发送消息
- SpringBoot webSocket 发送广播、点对点消息,Android接收
- SpringBoot-WebSocket广播消息+单点消息(指定用户发送消息)
- SpringBoot 实现WebSocket进行消息发送(适用于SpringMVC)
- WorkerMan学习篇:websocket+workerman聊天功能(三):点对点发送消息模拟
- workerMan学习篇:websocket+workerman聊天功能(三):点对点发送消息模拟
- socket 实现点对点发送消息
- SpringBoot中的WebSocket广播
- ActiveMQ发送消息,接受消息。点对点连接
- WebSocket点对点通信-Tomcat websocket点对点通信-实时通信-服务器消息推送
- SpringBoot学习-(十四)SpringBoot中建立WebSocket连接(STOMP实现发送消息给指定用户)
- ActiveMq点对点模式发送、接收消息
- spring websocket 模拟发送消息
- springboot+websocket+sockjs进行消息推送
- WorkerMan学习篇:websocket+workerman聊天功能(三):点对点发送消息模拟(亲测可用_准备尝试着做客服接口)
- UDP实现内网到网的点对点发送消息与转发消息
- .NET Compact Framework 中的点对点消息队列
- .NET Compact Framework 中的点对点消息队列
- Tensorflow学习之卷积神经网络实现(四)
- _MSC_VER
- 正弦函数的python显示
- Android 数据存储(三) 数据库存储
- yii2底层源码分析1
- SpringBoot中的WebSocket点对点发送消息
- Centos6 升级Git版本
- C++中的explicit关键字
- numpy: np.random模块 探究(源码)
- 【互联网安全】阿里云安全专家为你详细解读内容安全及防护
- celery分布式队列任务
- 疯狂Android讲义(第3版)学习笔记(第四章---Activity与Fragment)
- 深入理解Apache Flink核心技术
- JAVA必背面试题和项目面试通关要点