20231121版本,未完成
This commit is contained in:
parent
e6dd15a9e7
commit
1ddc34d25e
6
pom.xml
6
pom.xml
|
@ -62,6 +62,12 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!--json-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>2.0.32</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,9 @@ package com.example.chat;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||||
|
|
||||||
|
@EnableWebSocket
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class ChatApplication {
|
public class ChatApplication {
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
package com.example.chat.common;
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.example.chat.common.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebSocketConfig {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint
|
||||||
|
*
|
||||||
|
* */
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServerEndpointExporter serverEndpointExporter() {
|
||||||
|
return new ServerEndpointExporter();
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,7 +9,7 @@ import org.springframework.stereotype.Repository;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class Login implements UserTable {
|
public class Login implements UserMapper {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addUser(User user) throws IOException {
|
public void addUser(User user) throws IOException {
|
||||||
|
|
|
@ -22,7 +22,7 @@ public class Test01 implements CommandLineRunner {
|
||||||
}
|
}
|
||||||
sqlSession.close();
|
sqlSession.close();
|
||||||
System.out.println("sleep");
|
System.out.println("sleep");
|
||||||
TimeUnit.HOURS.sleep(2);
|
TimeUnit.HOURS.sleep(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@Repository
|
@Repository
|
||||||
public interface UserTable {
|
public interface UserMapper {
|
||||||
public abstract void addUser(User user) throws IOException;
|
public abstract void addUser(User user) throws IOException;
|
||||||
|
|
||||||
public abstract void delUser(User user);
|
public abstract void delUser(User user);
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.example.chat.dao.chat;
|
||||||
|
|
||||||
|
import com.example.chat.entity.chat.Message;
|
||||||
|
import com.example.chat.entity.chat.UpdateMessage;
|
||||||
|
import io.netty.handler.codec.MessageAggregator;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
@Repository
|
||||||
|
public interface ChatMapper {
|
||||||
|
public abstract void addMessage(Message message) throws IOException;
|
||||||
|
|
||||||
|
public abstract void deleteMessage();
|
||||||
|
|
||||||
|
public abstract List<Message> getMessage() throws IOException;
|
||||||
|
|
||||||
|
public abstract void updateMessage(UpdateMessage updateMessage) throws IOException;
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.example.chat.dao.chat;
|
||||||
|
|
||||||
|
import com.example.chat.dao.mybatis.MybatisSingleton;
|
||||||
|
import com.example.chat.entity.chat.Message;
|
||||||
|
import com.example.chat.entity.chat.UpdateMessage;
|
||||||
|
import org.apache.ibatis.session.SqlSession;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class ChatMessage implements ChatMapper {
|
||||||
|
@Override
|
||||||
|
public void addMessage(Message message) throws IOException {
|
||||||
|
SqlSession sqlSession = MybatisSingleton.getSqlSessionFactory().openSession();
|
||||||
|
sqlSession.insert("dao.chat.Message.addMessage", message);
|
||||||
|
sqlSession.commit();
|
||||||
|
sqlSession.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateMessage(UpdateMessage updateMessage) throws IOException {
|
||||||
|
SqlSession sqlSession = MybatisSingleton.getSqlSessionFactory().openSession();
|
||||||
|
sqlSession.update("dao.chat.Message.updateMessage", updateMessage);
|
||||||
|
sqlSession.commit();
|
||||||
|
sqlSession.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Message> getMessage() throws IOException {
|
||||||
|
List<Message> messageList;
|
||||||
|
SqlSession sqlSession = MybatisSingleton.getSqlSessionFactory().openSession();
|
||||||
|
messageList = sqlSession.selectOne("dao.chat.Message.getMessage");
|
||||||
|
sqlSession.close();
|
||||||
|
return messageList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteMessage() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,102 @@
|
||||||
|
package com.example.chat.entity.chat;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.annotation.JSONCreator;
|
||||||
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
|
||||||
|
public class Message {
|
||||||
|
private String nickname;
|
||||||
|
private String pic;
|
||||||
|
private String sendTime;
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
@JSONCreator
|
||||||
|
public static Message create(@JSONField(name= "nickname")String nickname,
|
||||||
|
@JSONField(name= "pic")String pic,
|
||||||
|
@JSONField(name= "sendTime")String sendTime,
|
||||||
|
@JSONField(name= "message")String message){
|
||||||
|
Message message1=new Message();
|
||||||
|
message1.setNickname(nickname);
|
||||||
|
message1.setMessage(message);
|
||||||
|
message1.setPic(pic);
|
||||||
|
message1.setSendTime(sendTime);
|
||||||
|
return message1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message(String nickName, String pic, String time, String message) {
|
||||||
|
this.nickname = nickName;
|
||||||
|
this.pic = pic;
|
||||||
|
this.sendTime = time;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取
|
||||||
|
* @return nickName
|
||||||
|
*/
|
||||||
|
public String getNickname() {
|
||||||
|
return nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置
|
||||||
|
* @param nickname
|
||||||
|
*/
|
||||||
|
public void setNickname(String nickname) {
|
||||||
|
this.nickname = nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取
|
||||||
|
* @return pic
|
||||||
|
*/
|
||||||
|
public String getPic() {
|
||||||
|
return pic;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置
|
||||||
|
* @param pic
|
||||||
|
*/
|
||||||
|
public void setPic(String pic) {
|
||||||
|
this.pic = pic;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取
|
||||||
|
* @return time
|
||||||
|
*/
|
||||||
|
public String getSendTime() {
|
||||||
|
return sendTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置
|
||||||
|
* @param sendTime
|
||||||
|
*/
|
||||||
|
public void setSendTime(String sendTime) {
|
||||||
|
this.sendTime = sendTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取
|
||||||
|
* @return message
|
||||||
|
*/
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置
|
||||||
|
* @param message
|
||||||
|
*/
|
||||||
|
public void setMessage(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "Message{nickName = " + nickname + ", pic = " + pic + ", time = " + sendTime + ", message = " + message + "}";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
package com.example.chat.entity.chat;
|
||||||
|
|
||||||
|
public class UpdateMessage {
|
||||||
|
|
||||||
|
private String nickname;
|
||||||
|
private String pic;
|
||||||
|
private String newName;
|
||||||
|
private String newPic;
|
||||||
|
|
||||||
|
|
||||||
|
public UpdateMessage() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public UpdateMessage(String nickname, String pic, String newName, String newPic) {
|
||||||
|
this.nickname = nickname;
|
||||||
|
this.pic = pic;
|
||||||
|
this.newName = newName;
|
||||||
|
this.newPic = newPic;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取
|
||||||
|
* @return nickname
|
||||||
|
*/
|
||||||
|
public String getNickname() {
|
||||||
|
return nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置
|
||||||
|
* @param nickname
|
||||||
|
*/
|
||||||
|
public void setNickname(String nickname) {
|
||||||
|
this.nickname = nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取
|
||||||
|
* @return pic
|
||||||
|
*/
|
||||||
|
public String getPic() {
|
||||||
|
return pic;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置
|
||||||
|
* @param pic
|
||||||
|
*/
|
||||||
|
public void setPic(String pic) {
|
||||||
|
this.pic = pic;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取
|
||||||
|
* @return newName
|
||||||
|
*/
|
||||||
|
public String getNewName() {
|
||||||
|
return newName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置
|
||||||
|
* @param newName
|
||||||
|
*/
|
||||||
|
public void setNewName(String newName) {
|
||||||
|
this.newName = newName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取
|
||||||
|
* @return newPic
|
||||||
|
*/
|
||||||
|
public String getNewPic() {
|
||||||
|
return newPic;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置
|
||||||
|
* @param newPic
|
||||||
|
*/
|
||||||
|
public void setNewPic(String newPic) {
|
||||||
|
this.newPic = newPic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "UpdateMessage{nickname = " + nickname + ", pic = " + pic + ", newName = " + newName + ", newPic = " + newPic + "}";
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
package com.example.chat.service;
|
package com.example.chat.service;
|
||||||
|
|
||||||
import com.auth0.jwt.JWT;
|
import com.example.chat.dao.UserMapper;
|
||||||
import com.example.chat.dao.UserTable;
|
|
||||||
import com.example.chat.dao.redis.RedisUtil;
|
import com.example.chat.dao.redis.RedisUtil;
|
||||||
import com.example.chat.entity.User;
|
import com.example.chat.entity.User;
|
||||||
import com.example.chat.jwt.JWTUtil;
|
import com.example.chat.jwt.JWTUtil;
|
||||||
|
@ -14,7 +13,7 @@ import java.io.IOException;
|
||||||
@Service
|
@Service
|
||||||
public class LoginService {
|
public class LoginService {
|
||||||
@Resource
|
@Resource
|
||||||
UserTable userTable;
|
UserMapper userTable;
|
||||||
@Resource(name = "tokenRedis")
|
@Resource(name = "tokenRedis")
|
||||||
RedisUtil redisUtil;
|
RedisUtil redisUtil;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
package com.example.chat.service.websocket;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.example.chat.dao.chat.ChatMapper;
|
||||||
|
import com.example.chat.entity.chat.Message;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.websocket.*;
|
||||||
|
import javax.websocket.server.ServerEndpoint;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@ServerEndpoint(value = "/chat/get")
|
||||||
|
@Service
|
||||||
|
public class WebSocketServer {
|
||||||
|
|
||||||
|
private final List<Session> sessionList = new ArrayList<>();
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
ChatMapper chatMapper;
|
||||||
|
|
||||||
|
@OnOpen
|
||||||
|
public void onOpen(Session session) throws IOException {
|
||||||
|
sessionList.add(session);
|
||||||
|
log.info("有新用户加入聊天,当前在线人数为{}", sessionList.size());
|
||||||
|
List<Message> messageList = chatMapper.getMessage();
|
||||||
|
sendAllSession(messageList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(String message) throws IOException {
|
||||||
|
Message message1= JSON.parseObject(message,Message.class);
|
||||||
|
chatMapper.addMessage(message1);
|
||||||
|
List<Message> messageList=chatMapper.getMessage();
|
||||||
|
sendAllSession(messageList);
|
||||||
|
}
|
||||||
|
@OnError
|
||||||
|
public void onError(Throwable error){
|
||||||
|
log.error("发生错误");
|
||||||
|
error.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnClose
|
||||||
|
public void onClose(Session session){
|
||||||
|
log.info("有一个用户断开连接了");
|
||||||
|
sessionList.remove(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 发消息给客户端
|
||||||
|
* */
|
||||||
|
public void sendAllSession(List<Message> message) throws IOException {
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (Session session : sessionList) {
|
||||||
|
log.info("给客户端发送消息{}",session);
|
||||||
|
session.getBasicRemote().sendObject(message);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("消息发送失败", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
|
<mapper namespace="dao.chat.Message">
|
||||||
|
<select id="getMessage" resultType="com.example.chat.entity.chat.Message">
|
||||||
|
select *
|
||||||
|
from message
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<insert id="addMessage" parameterType="com.example.chat.entity.chat.Message">
|
||||||
|
insert into message (nickname, pic, sendTime, message)
|
||||||
|
values (#{nickname}, #{pic}, #{sendTime}, #{message})
|
||||||
|
</insert>
|
||||||
|
|
||||||
|
<update id="updateUser" parameterType="com.example.chat.entity.chat.UpdateMessage">
|
||||||
|
update message
|
||||||
|
set nickname=#{newName} and pic = #{newPic}
|
||||||
|
where nickname = #{nickname}
|
||||||
|
and pic = #{pic}
|
||||||
|
</update>
|
||||||
|
|
||||||
|
</mapper>
|
|
@ -22,5 +22,6 @@
|
||||||
<mappers>
|
<mappers>
|
||||||
<!--下面编写mapper映射文件↓↓↓↓↓ 参考格式:<VueLoginMapper.xml resource="dao/VueLoginMapper.xml"/> -->
|
<!--下面编写mapper映射文件↓↓↓↓↓ 参考格式:<VueLoginMapper.xml resource="dao/VueLoginMapper.xml"/> -->
|
||||||
<mapper resource="mapper/LoginMapper.xml"></mapper>
|
<mapper resource="mapper/LoginMapper.xml"></mapper>
|
||||||
|
<mapper resource="mapper/MessageMapper.xml"></mapper>
|
||||||
</mappers>
|
</mappers>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|
Loading…
Reference in New Issue