use of com.alibaba.fastjson2.JSONException in project witsTalk by XinSin-top.
the class ChatFrameHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) {
/*
此方法会在收到消息时调用
*/
//
session = ChatStart.factory.openSession();
chatDao = session.getMapper(ChatDao.class);
// 获取传输通道
Channel channel = channelHandlerContext.channel();
// 获取通道ID
ChannelId id = channel.id();
// 获取消息文本内容
String data = textWebSocketFrame.text();
// 日志
log.debug("receive message {} ip = {} id = {}", data, channel.remoteAddress().toString(), id.asShortText());
// 将消息转换为json
JSONObject json;
try {
json = JSON.parseObject(data);
} catch (JSONException e) {
// 打印错误
e.printStackTrace();
// 将syntax error发回至客户端
sendMessage(id, "syntax error");
return;
}
// 获取进行的操作
String operating = json.getString("op");
// 获取操作的参数
JSONObject args = json.getJSONObject("args");
String loginCommand = "login";
String sendCommand = "send";
String getMessageCommand = "get";
String getMessageCountCommand = "count";
String recallMessageCommand = "recall";
// 登录操作
if (Objects.equals(operating, loginCommand)) {
Boolean r = JwtTokenUtils.isRight(args.getString("token"));
log.debug("logon,r = {} ip = {} id = {}", r, channel.remoteAddress().toString(), id.asShortText());
loginTable.put(id, r);
sendMessage(id, r.toString());
} else if (loginTable.get(id)) {
// 如果操作为send
if (Objects.equals(operating, sendCommand)) {
// 获取发送者
String sender = args.getString("sender");
// 获取发送内容
String content = args.getString("content");
// 获取发送的类型
String type = args.getString("type");
// 日志
log.info("sendMessage,sender = {} content = {} type = {}", sender, content, type);
// 定义消息对象
Message message = new Message(content, sender, type);
// Dao层发送消息
chatDao.sendMessage(message);
session.commit();
// 获取用户头像
String b64 = chatDao.getUserHeadPortrait(sender);
message.setBase64(b64);
// 广播此消息
String rawJson = JSON.toJSONString(message);
sendToAll(rawJson);
} else // 如果操作为getMessage
if (Objects.equals(operating, getMessageCommand)) {
// 获取id最小值
Integer min = args.getInteger("min");
// 获取id最大值
Integer max = args.getInteger("max");
// 日志
log.info("getMessage min = {} max = {} ip = {} id = {}", min, max, channel.remoteAddress(), id.asShortText());
// 从数据库获取消息
ArrayList<Message> messages = chatDao.getMessage(min, max);
// todo 更改为双标查询获取头像
// 将这些消息转换为json并返回给客户端
sendMessage(id, JSON.toJSONString(messages));
} else // 如果操作为getMessageCount
if (Objects.equals(operating, getMessageCountCommand)) {
// 日志
log.info("getMessageCount");
// 从数据库获取消息数量
Integer count = chatDao.getCount().get(0);
sendMessage(id, count);
} else if (Objects.equals(operating, recallMessageCommand)) {
int i = args.getInteger("id");
Message message = new Message(i);
log.info("Recall Message message={}", message);
chatDao.recall(message);
session.commit();
HashMap<String, String> resp = new HashMap<>(10);
resp.put("op", "recall");
resp.put("id", String.valueOf(i));
sendToAll(JSON.toJSONString(resp));
}
} else {
sendMessage(id, "You are not login!");
}
session.close();
}
Aggregations