Websocket点对点方式实现简单聊天功能
程序员文章站
2022-03-10 12:16:12
...
一 小项目介绍
演示一个简单的聊天程序。例子中只有两个用户,互相发送消息给彼此。
二 实战
1 新建spring boot项目
2 编写pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
3 Spring Security配置
package com.wisely.ch7_6;
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
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/","/login").permitAll()//设置Spring Security对/和/login路径不拦截
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login") //设置登陆页面访问的路径为/login
.defaultSuccessUrl("/chat") //登陆成功转向该页面
.permitAll()
.and()
.logout()
.permitAll();
}
//在内存中分配配置两个用户wyf和wisely,用户名和密码相等,角色都是USER
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("wyf").password("wyf").roles("USER")
.and()
.withUser("wisely").password("wisely").roles("USER");
}
//忽略静态资源的拦截
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/static/**");
}
}
4 配置WebSocket
package com.wisely.ch7_6;
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
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/endpointChat").withSockJS();//注册一个名为/endpointChat的端点
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/queue"); //点对点增加一个/queue消息代理
}
}
5 编写控制器
package com.wisely.ch7_6.web;
import java.security.Principal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import com.wisely.ch7_6.domain.WiselyMessage;
import com.wisely.ch7_6.domain.WiselyResponse;
@Controller
public class WsController {
@Autowired
private SimpMessagingTemplate messagingTemplate;//通过SimpMessagingTemplate向浏览器发送消息
@MessageMapping("/chat")
//在Spring MVC中,可以直接在参数中获得Principal,Principal包含了当前用户的信息
public void handleChat(Principal principal, String msg) {
//如果发送人是wyf,则发送给wisely,如果发送人是wisely,则发送给wyf
if (principal.getName().equals("wyf")) {
//通过convertAndSendToUser向用户发送消息
//第1个参数:接收消息的用户
//第2个参数:浏览器订阅的地址
//第3个参数:消息本身
messagingTemplate.convertAndSendToUser("wisely",
"/queue/notifications", principal.getName() + "-send:"
+ msg);
} else {
messagingTemplate.convertAndSendToUser("wyf",
"/queue/notifications", principal.getName() + "-send:"
+ msg);
}
}
}
6 编写登录页面
<!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" value="登陆"/></div>
</form>
</body>
</html>
7 编写聊天页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8" />
<head>
<title>Home</title>
<script th:src="@{sockjs.min.js}"></script>
<script th:src="@{stomp.min.js}"></script>
<script th:src="@{jquery.js}"></script>
</head>
<body>
<p>
聊天室
</p>
<form id="wiselyForm">
<textarea rows="4" cols="60" name="text"></textarea>
<input type="submit"/>
</form>
<script th:inline="javascript">
$('#wiselyForm').submit(function(e){
e.preventDefault();
var text = $('#wiselyForm').find('textarea[name="text"]').val();
sendSpittle(text);
});
var sock = new SockJS("/endpointChat"); //连接的端点名称为/endpointChat
var stomp = Stomp.over(sock);
stomp.connect('guest', 'guest', function(frame) {
//订阅/user/queue/notifications发送的消息,这里与在控制器的
//messagingTemplate.convertAndSendToUser中定义的订阅地址保持一致。
//这里多了一个/user是必须的,使用/user才会发送消息到指定用户
stomp.subscribe("/user/queue/notifications", handleNotification);
});
function handleNotification(message) {
$('#output').append("<b>Received: " + message.body + "</b><br/>")
}
function sendSpittle(text) {
stomp.send("/chat", {}, text);//发送消息
}
$('#stop').click(function() {sock.close()});
</script>
<div id="output"></div>
</body>
</html>
8 增加页面viewController
package com.wisely.ch7_6;
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("/login").setViewName("/login");
registry.addViewController("/chat").setViewName("/chat");
}
}
三 运行
1 分别打开google浏览器和其他一个支持websocket的浏览器。
2 两个浏览器一个用wfy登录,一个用wisely登录。
3 两个浏览器互发消息,达到聊天的效果。