老师,为什么要判断两次呢?

来源:9-17 chatHandler - 处理消息发送

zbbest

2021-03-14

// 发送消息
// 从全局用户Channel关系中获取接受方的channel
Channel receiverChannel = UserChannelRel.get(receiverId);
if (receiverChannel == null) {
    // TODO channel为空代表用户离线,推送消息(JPush,个推,小米推送)
} else {
    // 当receiverChannel不为空的时候,从ChannelGroup去查找对应的channel是否存在
    Channel findChannel = users.find(receiverChannel.id());
    if (findChannel != null) {
         // 用户在线
         receiverChannel.writeAndFlush(
		new TextWebSocketFrame(
			JsonUtils.objectToJson(dataContentMsg)));
		} else {
		// 用户离线 TODO 推送消息
	}
}
  • Channel findChannel = users.find(receiverChannel.id()); 这个 findChannel 不就是上面的 receiverChannel 吗?还是完全不一样的 channel 呢?

  • 如果一样的话,还要判断 if (findChannel != null) 吗?

写回答

1回答

风间影月

2021-03-14

只要是对象。一般都要加多一个判断的

0
1
zbbest
非常感谢!
2021-03-14
共1条回复

Netty+Spring Boot仿微信-全栈开发高性能后台及客户端

SpringBoot/Netty+MUI全栈开发 同时搞定后台+ Android&iOS

1498 学习 · 684 问题

查看课程