一坨屎,jsonObject有bug
This commit is contained in:
parent
1ddc34d25e
commit
1c84d4faa4
8
pom.xml
8
pom.xml
|
@ -21,6 +21,10 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.auth0</groupId>
|
<groupId>com.auth0</groupId>
|
||||||
<artifactId>java-jwt</artifactId>
|
<artifactId>java-jwt</artifactId>
|
||||||
|
@ -66,9 +70,11 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
<artifactId>fastjson</artifactId>
|
<artifactId>fastjson</artifactId>
|
||||||
<version>2.0.32</version>
|
<version>1.2.83</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package com.example.chat;
|
package com.example.chat;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
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;
|
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||||
|
|
||||||
@EnableWebSocket
|
//@EnableWebSocket
|
||||||
|
@Mapper
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class ChatApplication {
|
public class ChatApplication {
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
package com.example.chat.controller;
|
package com.example.chat.controller;
|
||||||
|
|
||||||
|
import com.example.chat.ChatApplication;
|
||||||
|
import com.example.chat.dao.chat.ChatMapper;
|
||||||
import com.example.chat.entity.Email;
|
import com.example.chat.entity.Email;
|
||||||
import com.example.chat.entity.Result;
|
import com.example.chat.entity.Result;
|
||||||
import com.example.chat.entity.User;
|
import com.example.chat.entity.User;
|
||||||
|
import com.example.chat.entity.chat.Message;
|
||||||
import com.example.chat.service.CodeCheck;
|
import com.example.chat.service.CodeCheck;
|
||||||
import com.example.chat.service.LoginService;
|
import com.example.chat.service.LoginService;
|
||||||
import com.example.chat.service.mail.MailUtil;
|
import com.example.chat.service.mail.MailUtil;
|
||||||
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@ -22,6 +26,8 @@ public class LoginController {
|
||||||
@Resource
|
@Resource
|
||||||
MailUtil mailUtil;
|
MailUtil mailUtil;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public Result Login(@RequestBody User user) throws IOException {
|
public Result Login(@RequestBody User user) throws IOException {
|
||||||
Result result = new Result();
|
Result result = new Result();
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.example.chat.dao;
|
||||||
import com.example.chat.entity.User;
|
import com.example.chat.entity.User;
|
||||||
import com.example.chat.dao.mybatis.MybatisSingleton;
|
import com.example.chat.dao.mybatis.MybatisSingleton;
|
||||||
import org.apache.ibatis.session.SqlSession;
|
import org.apache.ibatis.session.SqlSession;
|
||||||
|
import org.junit.Test;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@Repository
|
|
||||||
public interface ChatMapper {
|
public interface ChatMapper {
|
||||||
public abstract void addMessage(Message message) throws IOException;
|
public abstract void addMessage(Message message) throws IOException;
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import org.apache.ibatis.session.SqlSession;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
|
@ -28,10 +27,11 @@ public class ChatMessage implements ChatMapper {
|
||||||
sqlSession.close();
|
sqlSession.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public List<Message> getMessage() throws IOException {
|
public List<Message> getMessage() throws IOException {
|
||||||
List<Message> messageList;
|
List<Message> messageList;
|
||||||
SqlSession sqlSession = MybatisSingleton.getSqlSessionFactory().openSession();
|
SqlSession sqlSession = MybatisSingleton.getSqlSessionFactory().openSession();
|
||||||
messageList = sqlSession.selectOne("dao.chat.Message.getMessage");
|
messageList = sqlSession.selectList("dao.chat.Message.getMessage");
|
||||||
sqlSession.close();
|
sqlSession.close();
|
||||||
return messageList;
|
return messageList;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.example.chat.dao.chat;
|
||||||
|
|
||||||
|
import com.example.chat.ChatApplication;
|
||||||
|
import com.example.chat.dao.UserMapper;
|
||||||
|
import com.example.chat.entity.User;
|
||||||
|
import com.example.chat.entity.chat.Message;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.IOException;
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = ChatApplication.class)
|
||||||
|
public class MapperTest {
|
||||||
|
|
||||||
|
@Resource(name = "chatMessage")
|
||||||
|
ChatMapper mapper;
|
||||||
|
@Resource(name = "login")
|
||||||
|
UserMapper userMapper;
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void Test02() throws IOException {
|
||||||
|
System.out.println(userMapper.getUser(new User("1", "1", "1", "1")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void Test01() throws IOException {
|
||||||
|
mapper.addMessage(new Message("1", "1", "1", "1"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,6 +24,7 @@ public class InterceptorConfig implements WebMvcConfigurer {
|
||||||
excludePath.add("/sendCode");
|
excludePath.add("/sendCode");
|
||||||
excludePath.add("/register");//注册
|
excludePath.add("/register");//注册
|
||||||
excludePath.add("/register");
|
excludePath.add("/register");
|
||||||
|
excludePath.add("/chat/get");
|
||||||
registry.addInterceptor(tokenInterceptor)//注册拦截器
|
registry.addInterceptor(tokenInterceptor)//注册拦截器
|
||||||
.addPathPatterns("/**")//拦截所有请求
|
.addPathPatterns("/**")//拦截所有请求
|
||||||
.excludePathPatterns(excludePath);//添加拦截白名单
|
.excludePathPatterns(excludePath);//添加拦截白名单
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.example.chat.dao.UserMapper;
|
||||||
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;
|
||||||
|
import org.junit.Test;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
@ -17,14 +18,17 @@ public class LoginService {
|
||||||
@Resource(name = "tokenRedis")
|
@Resource(name = "tokenRedis")
|
||||||
RedisUtil redisUtil;
|
RedisUtil redisUtil;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void Test01() throws IOException {
|
||||||
|
System.out.println(userTable.getUser(new User()));
|
||||||
|
}
|
||||||
|
|
||||||
public String LoginCheck(User requestUser) throws IOException {
|
public String LoginCheck(User requestUser) throws IOException {
|
||||||
User user;
|
User user;
|
||||||
user = userTable.getUser(requestUser);
|
user = userTable.getUser(requestUser);
|
||||||
System.out.println(user);
|
System.out.println(user);
|
||||||
System.out.println(requestUser);
|
System.out.println(requestUser);
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
|
|
||||||
|
|
||||||
if (user.getPassword().equals(requestUser.getPassword())) {
|
if (user.getPassword().equals(requestUser.getPassword())) {
|
||||||
String token = JWTUtil.sign(user.getAccount());
|
String token = JWTUtil.sign(user.getAccount());
|
||||||
redisUtil.addRedis(user.getAccount(), token);
|
redisUtil.addRedis(user.getAccount(), token);
|
||||||
|
|
|
@ -1,54 +1,57 @@
|
||||||
package com.example.chat.service.websocket;
|
package com.example.chat.service.websocket;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.example.chat.dao.chat.ChatMapper;
|
import com.example.chat.dao.chat.ChatMapper;
|
||||||
|
import com.example.chat.dao.chat.ChatMessage;
|
||||||
import com.example.chat.entity.chat.Message;
|
import com.example.chat.entity.chat.Message;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import javax.swing.*;
|
|
||||||
import javax.websocket.*;
|
import javax.websocket.*;
|
||||||
import javax.websocket.server.ServerEndpoint;
|
import javax.websocket.server.ServerEndpoint;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
|
@CrossOrigin
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ServerEndpoint(value = "/chat/get")
|
@ServerEndpoint(value = "/chat/get")
|
||||||
@Service
|
@Component
|
||||||
public class WebSocketServer {
|
public class WebSocketServer {
|
||||||
|
|
||||||
private final List<Session> sessionList = new ArrayList<>();
|
private static final List<Session> sessionList = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
@Resource
|
|
||||||
ChatMapper chatMapper;
|
public ChatMapper chatMapper=new ChatMessage();
|
||||||
|
|
||||||
@OnOpen
|
@OnOpen
|
||||||
public void onOpen(Session session) throws IOException {
|
public void onOpen(Session session) throws IOException {
|
||||||
sessionList.add(session);
|
sessionList.add(session);
|
||||||
log.info("有新用户加入聊天,当前在线人数为{}", sessionList.size());
|
log.info("有新用户加入聊天,当前在线人数为{}", sessionList.size());
|
||||||
|
if (chatMapper == null) {
|
||||||
|
log.error("chatMapper为null");
|
||||||
|
}
|
||||||
List<Message> messageList = chatMapper.getMessage();
|
List<Message> messageList = chatMapper.getMessage();
|
||||||
sendAllSession(messageList);
|
sendAllSession(messageList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnMessage
|
@OnMessage
|
||||||
public void onMessage(String message) throws IOException {
|
public void onMessage(String message) throws IOException {
|
||||||
Message message1= JSON.parseObject(message,Message.class);
|
Message message1 = JSON.parseObject(message, Message.class);
|
||||||
chatMapper.addMessage(message1);
|
chatMapper.addMessage(message1);
|
||||||
List<Message> messageList=chatMapper.getMessage();
|
List<Message> messageList = chatMapper.getMessage();
|
||||||
sendAllSession(messageList);
|
sendAllSession(messageList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnError
|
@OnError
|
||||||
public void onError(Throwable error){
|
public void onError(Throwable error) {
|
||||||
log.error("发生错误");
|
log.error("发生错误");
|
||||||
error.printStackTrace();
|
error.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnClose
|
@OnClose
|
||||||
public void onClose(Session session){
|
public void onClose(Session session) {
|
||||||
log.info("有一个用户断开连接了");
|
log.info("有一个用户断开连接了");
|
||||||
sessionList.remove(session);
|
sessionList.remove(session);
|
||||||
}
|
}
|
||||||
|
@ -60,8 +63,8 @@ public class WebSocketServer {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (Session session : sessionList) {
|
for (Session session : sessionList) {
|
||||||
log.info("给客户端发送消息{}",session);
|
log.info("给客户端发送消息{}", session);
|
||||||
session.getBasicRemote().sendObject(message);
|
session.getBasicRemote().sendText(JSON.toJSONString(message));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("消息发送失败", e);
|
log.error("消息发送失败", e);
|
||||||
|
|
Loading…
Reference in New Issue