Netty+SpringBoot+FastDFS+Html5实现聊天App详解(五)

本文介绍了使用Netty、SpringBoot和FastDFS构建聊天App的过程,包括消息实体类、发送消息枚举、用户与channel关系记录、异常处理以及未签收消息接口的实现。详细讲解了读取、处理不同类型的聊天消息,以及如何签收和发送消息。还提供了获取未签收消息列表的接口,并进行了测试。
摘要由CSDN通过智能技术生成

Netty+SpringBoot+FastDFS+Html5实现聊天App,项目介绍

Netty+SpringBoot+FastDFS+Html5实现聊天App,项目github链接

本章完整代码链接

本章主要讲的是聊天App_PigChat中关于聊天功能的实现。



移除方法与处理异常方法的重写

在ChatHandler中重写其移除channel的方法handlerRemoved,以及处理异常的方法exceptionCaught。

	@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
		
		String channelId = ctx.channel().id().asShortText();
		System.out.println("客户端被移除,channelId为:" + channelId);
		
		// 当触发handlerRemoved,ChannelGroup会自动移除对应客户端的channel
		users.remove(ctx.channel());
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		// 发生异常之后关闭连接(关闭channel),随后从ChannelGroup中移除
		ctx.channel().close();
		users.remove(ctx.channel());
	}



定义消息的实体类

public class ChatMsg implements Serializable {

	private static final long serialVersionUID = 3611169682695799175L;
	
	private String senderId;		// 发送者的用户id	
	private String receiverId;		// 接受者的用户id
	private String msg;				// 聊天内容
	private String msgId;			// 用于消息的签收
	
	public String getSenderId() {
		return senderId;
	}
	public void setSenderId(String senderId) {
		this.senderId = senderId;
	}
	public String getReceive
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值