前言

上篇博文(netty实现消息中心(一)思路整理

)大概说了下netty websocket消息中心的设计思路,这篇文章主要说说简化版的netty聊天室代码实现,支持群聊和点对点聊天。

此demo主要说明netty实现消息推送的基本使用方法,如果需要扩充其它功能,可以基于此脚手架扩展。

完整项目代码地址:netty聊天室github源码

介绍

1.登录页面

2.聊天页面

核心代码:

启动netty服务,监听端口

    private static void startNettyMsgServer() {
// 使用多Reactor多线程模型,EventLoopGroup相当于线程池,内部维护一个或多个线程(EventLoop),每个EventLoop可处理多个Channel(单线程处理多个IO任务)
// 创建主线程组EventLoopGroup,专门负责建立连接
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
// 创建子线程组,专门负责IO任务的处理
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workGroup);
b.channel(NioServerSocketChannel.class);
b.childHandler(new WebSocketChanneInitializer());
System.out.println("服务端开启等待客户端连接....");
Channel ch = b.bind(WebSocketConstant.WEB_SOCKET_PORT).sync().channel(); //创建一个定长线程池,支持定时及周期性任务执行
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
WebSocketInfoService webSocketInfoService = new WebSocketInfoService();
//定时任务:扫描所有的Channel,关闭失效的Channel
executorService.scheduleAtFixedRate(webSocketInfoService::scanNotActiveChannel,
3, 60, TimeUnit.SECONDS); //定时任务:向所有客户端发送Ping消息
executorService.scheduleAtFixedRate(webSocketInfoService::sendPing,
3, 50, TimeUnit.SECONDS); ch.closeFuture().sync(); } catch (Exception e) {
e.printStackTrace();
} finally {
// //退出程序
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}

netty ChannelHandler,负责处理通道的生命周期事件

package com.cola.chat_server.handler;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSONObject;
import com.cola.chat_server.constant.MessageCodeConstant;
import com.cola.chat_server.constant.MessageTypeConstant;
import com.cola.chat_server.constant.WebSocketConstant;
import com.cola.chat_server.model.WsMessage;
import com.cola.chat_server.service.WebSocketInfoService;
import com.cola.chat_server.util.DateUtils;
import com.cola.chat_server.util.NettyAttrUtil;
import com.cola.chat_server.util.RequestParamUtil;
import com.cola.chat_server.util.SessionHolder; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
import io.netty.util.CharsetUtil; /**
* Netty ChannelHandler,用来处理客户端和服务端的会话生命周期事件(握手、建立连接、断开连接、收消息等)
* @Author
* @Description 接收请求,接收 WebSocket 信息的控制类
*/
public class WebSocketSimpleChannelInboundHandler extends SimpleChannelInboundHandler<Object> { private static final Logger logger = LoggerFactory.getLogger(WebSocketSimpleChannelInboundHandler.class);
// WebSocket 握手工厂类
private WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(WebSocketConstant.WEB_SOCKET_URL, null, false);
private WebSocketServerHandshaker handshaker;
private WebSocketInfoService websocketInfoService = new WebSocketInfoService(); /**
* 处理客户端与服务端之间的 websocket 业务
*/
private void handWebsocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
//判断是否是关闭 websocket 的指令
if (frame instanceof CloseWebSocketFrame) {
//关闭握手
handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
websocketInfoService.clearSession(ctx.channel());
return;
}
//判断是否是ping消息
if (frame instanceof PingWebSocketFrame) {
ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
return;
}
// 判断是否Pong消息
if (frame instanceof PongWebSocketFrame) {
ctx.writeAndFlush(new PongWebSocketFrame(frame.content().retain()));
return;
}
//判断是否是二进制消息,如果是二进制消息,抛出异常
if (!(frame instanceof TextWebSocketFrame)) {
System.out.println("目前我们不支持二进制消息");
ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
throw new RuntimeException("【" + this.getClass().getName() + "】不支持消息");
}
// 获取并解析客户端向服务端发送的 json 消息
String message = ((TextWebSocketFrame) frame).text();
logger.info("消息:{}", message);
JSONObject json = JSONObject.parseObject(message);
try {
String uuid = UUID.randomUUID().toString();
String time = DateUtils.date2String(new Date(), "yyyy-MM-dd HH:mm:ss");
json.put("id", uuid);
json.put("sendTime", time); int code = json.getIntValue("code");
switch (code) {
//群聊
case MessageCodeConstant.GROUP_CHAT_CODE:
//向连接上来的客户端广播消息
SessionHolder.channelGroup.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(json)));
break;
//私聊
case MessageCodeConstant.PRIVATE_CHAT_CODE:
//接收人id
String receiveUserId = json.getString("receiverUserId");
String sendUserId = json.getString("sendUserId");
String msg = JSONObject.toJSONString(json);
// 点对点挨个给接收人发送消息
for (Map.Entry<String, Channel> entry : SessionHolder.channelMap.entrySet()) {
String userId = entry.getKey();
Channel channel = entry.getValue();
if (receiveUserId.equals(userId)) {
channel.writeAndFlush(new TextWebSocketFrame(msg));
}
}
// 如果发给别人,给自己也发一条
if (!receiveUserId.equals(sendUserId)) {
SessionHolder.channelMap.get(sendUserId).writeAndFlush(new TextWebSocketFrame(msg));
}
break;
case MessageCodeConstant.SYSTEM_MESSAGE_CODE:
//向连接上来的客户端广播消息
SessionHolder.channelGroup.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(json)));
break;
//pong
case MessageCodeConstant.PONG_CHAT_CODE:
Channel channel = ctx.channel();
// 更新心跳时间
NettyAttrUtil.refreshLastHeartBeatTime(channel);
default:
}
} catch(Exception e) {
logger.error("转发消息异常:", e);
e.printStackTrace();
}
} /**
* 客户端与服务端创建连接的时候调用
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
//创建新的 WebSocket 连接,保存当前 channel
logger.info("————客户端与服务端连接开启————");
// // 设置高水位
// ctx.channel().config().setWriteBufferHighWaterMark();
// // 设置低水位
// ctx.channel().config().setWriteBufferLowWaterMark();
} /**
* 客户端与服务端断开连接的时候调用
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("————客户端与服务端连接断开————");
websocketInfoService.clearSession(ctx.channel());
} /**
* 服务端接收客户端发送过来的数据结束之后调用
*/
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
} /**
* 工程出现异常的时候调用
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
logger.error("异常:", cause);
ctx.close();
} /**
* 服务端处理客户端websocket请求的核心方法
*/
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
if (o instanceof FullHttpRequest) {
//处理客户端向服务端发起 http 请求的业务
handHttpRequest(channelHandlerContext, (FullHttpRequest) o);
} else if (o instanceof WebSocketFrame) {
//处理客户端与服务端之间的 websocket 业务
handWebsocketFrame(channelHandlerContext, (WebSocketFrame) o);
}
} /**
* 处理客户端向服务端发起 http 握手请求的业务
* WebSocket在建立握手时,数据是通过HTTP传输的。但是建立之后,在真正传输时候是不需要HTTP协议的。
*
* WebSocket 连接过程:
* 首先,客户端发起http请求,经过3次握手后,建立起TCP连接;http请求里存放WebSocket支持的版本号等信息,如:Upgrade、Connection、WebSocket-Version等;
* 然后,服务器收到客户端的握手请求后,同样采用HTTP协议回馈数据;
* 最后,客户端收到连接成功的消息后,开始借助于TCP传输信道进行全双工通信。
*/
private void handHttpRequest(ChannelHandlerContext ctx, FullHttpRequest request) {
// 如果请求失败或者该请求不是客户端向服务端发起的 http 请求,则响应错误信息
if (!request.decoderResult().isSuccess()
|| !("websocket".equals(request.headers().get("Upgrade")))) {
// code :400
sendHttpResponse(ctx, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
return;
}
//新建一个握手
handshaker = factory.newHandshaker(request);
if (handshaker == null) {
//如果为空,返回响应:不受支持的 websocket 版本
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
//否则,执行握手
Map<String, String> params = RequestParamUtil.urlSplit(request.uri());
String userId = params.get("userId");
Channel channel = ctx.channel();
NettyAttrUtil.setUserId(channel, userId);
NettyAttrUtil.refreshLastHeartBeatTime(channel);
handshaker.handshake(ctx.channel(), request);
SessionHolder.channelGroup.add(ctx.channel());
SessionHolder.channelMap.put(userId, ctx.channel());
logger.info("握手成功,客户端请求uri:{}", request.uri()); // 推送用户上线消息,更新客户端在线用户列表
Set<String> userList = SessionHolder.channelMap.keySet();
WsMessage msg = new WsMessage();
Map<String, Object> ext = new HashMap<String, Object>();
ext.put("userList", userList);
msg.setExt(ext);
msg.setCode(MessageCodeConstant.SYSTEM_MESSAGE_CODE);
msg.setType(MessageTypeConstant.UPDATE_USERLIST_SYSTEM_MESSGAE);
SessionHolder.channelGroup.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(msg))); }
} /**
* 服务端向客户端响应消息
*/
private void sendHttpResponse(ChannelHandlerContext ctx, DefaultFullHttpResponse response) {
if (response.status().code() != 200) {
//创建源缓冲区
ByteBuf byteBuf = Unpooled.copiedBuffer(response.status().toString(), CharsetUtil.UTF_8);
//将源缓冲区的数据传送到此缓冲区
response.content().writeBytes(byteBuf);
//释放源缓冲区
byteBuf.release();
}
//写入请求,服务端向客户端发送数据
ChannelFuture channelFuture = ctx.channel().writeAndFlush(response);
if (response.status().code() != 200) {
/**
* 如果请求失败,关闭 ChannelFuture
* ChannelFutureListener.CLOSE 源码:future.channel().close();
*/
channelFuture.addListener(ChannelFutureListener.CLOSE);
}
}
}

会话工具类,保存用户和通道的对应关系,用于广播和点对点聊天

/**
* netty会话管理
* @author
*
*/
public class SessionHolder { /**
* 存储每个客户端接入进来时的 channel 对象
* 主要用于使用 writeAndFlush 方法广播信息
*/
public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); /**
* 用于客户端和服务端握手时存储用户id和netty Channel对应关系
*/
public static Map<String, Channel> channelMap = new ConcurrentHashMap<String, Channel>(); }

主要代码就是以上部分,如果需要扩充其它功能,可以基于此脚手架扩展。

完整项目代码地址:netty聊天室github源码

此demo主要用于展示netty实现消息推送的基本使用方法,用于生产还存在以下单机问题:

1.无法支撑过高连接数

2.广播时带宽有限

3.不能实现高可用

4.无法横向扩展

后期将集成zookeeper,做一版netty集群的聊天室。

netty实现消息中心(二)基于netty搭建一个聊天室的更多相关文章

  1. 使用 NIO 搭建一个聊天室

    使用 NIO 搭建一个聊天室 前面刚讲了使用 Socket 搭建了一个 Http Server,在最后我们使用了 NIO 对 Server 进行了优化,然后有小伙伴问到怎么使用 Socket 搭建聊天 ...

  2. netty实现消息中心(一)思路整理

    一.需求 需要实现直播间的以下功能: 群发消息(文本.图片.推荐商品) 点对点私发消息(文本.图片.推荐商品) 单个用户禁言 全体用户禁言 撤回消息 聊天记录持久化 二.技术实现 服务端消息中心采用n ...

  3. 如何基于Go搭建一个大数据平台

    如何基于Go搭建一个大数据平台 - Go中国 - CSDN博客 https://blog.csdn.net/ra681t58cjxsgckj31/article/details/78333775 01 ...

  4. 基于 Express 搭建一个node项目 - 起步

    一,如何基于 Express 搭建一个node项目 什么是Express 借用官方的介绍,Express是一个基于Node.js平台的极简.灵活的web应用开发框架,它提供了一系列强大的特性,帮助你创 ...

  5. 基于flask的网页聊天室(二)

    基于flask的网页聊天室(二) 前言 接上一次的内容继续完善,今天完成的内容不是很多,只是简单的用户注册登录,内容具体如下 具体内容 这次要加入与数据哭交互的操作,所以首先要建立相关表结构,这里使用 ...

  6. SilverLight搭建WCF聊天室详细过程[转]

    http://www.silverlightchina.net/html/zhuantixilie/getstart/2011/0424/7148.html 默认节点 SilverLight搭建WCF ...

  7. workerman-chat(PHP开发的基于Websocket协议的聊天室框架)(thinkphp也是支持socket聊天的)

    workerman-chat(PHP开发的基于Websocket协议的聊天室框架)(thinkphp也是支持socket聊天的) 一.总结 1.下面链接里面还有一个来聊的php聊天室源码可以学习 2. ...

  8. 基于springboot的websocket聊天室

    WebSocket入门 1.概述 1.1 Http #http简介 HTTP是一个应用层协议,无状态的,端口号为80.主要的版本有1.0/1.1/2.0. #http1.0/1.1/2.0 1.HTT ...

  9. 基于react+react-router+redux+socket.io+koa开发一个聊天室

    最近练手开发了一个项目,是一个聊天室应用.项目虽不大,但是使用到了react, react-router, redux, socket.io,后端开发使用了koa,算是一个比较综合性的案例,很多概念和 ...

随机推荐

  1. 【MySQL】剖析MySQL读写分离技术

    主从技术的一个基本流程图: 如何实现主从复制的呢: MySQL  Master(主节点) 1>当一个请求来时,首先由[mysqld]写入到我们的主[data]中 2>然后[mysqld]将 ...

  2. 二、第一个C程序:Hello World!

    如何在Dev C++中编辑.编译和运行程序? 一.打开Dev C++ 二.在上面编辑窗口中输入以下代码 #include<stdio.h> int main() { printf(&quo ...

  3. 母牛的故事(hdu2018)——笔记待完善

    思考:这道题考验的是罗辑思维,这个网址http://blog.csdn.net/sxhelijian/article/details/42609353的罗辑思维值得学习 #include<std ...

  4. 实验二、OpenCV图像滤波

    一.题目描述 对下面的图片进行滤波和边缘提取操作,请详细地记录每一步操作的步骤. 滤波操作可以用来过滤噪声,常见噪声有椒盐噪声和高斯噪声,椒盐噪声可以理解为斑点,随机出现在图像中的黑点或白点:高斯噪声 ...

  5. [开源] .Net orm FreeSql 1.5.0 最新版本(番号:好久不见)

    废话开头 这篇文章是我有史以来编辑最长时间的,历时 4小时!!!原本我可以利用这 4小时编写一堆胶水代码,真心希望善良的您点个赞,谢谢了!! 很久很久没有写文章了,上一次还是在元旦发布 1.0 版本的 ...

  6. C语言关于数据类型转换

    自动类型转换 自动类型转换就是编译器默默地.隐式地.偷偷地进行的数据类型转换,这种转换不需要程序员干预,会自动发生. 1) 将一种类型的数据赋值给另外一种类型的变量时就会发生自动类型转换,例如: ; ...

  7. Jmeter执行多个sql查询语句

    1.添加jdbc connection(注意标红部分) 2.添加jdbc request 3.查看结果树 本文主要向大家介绍了Oracle数据库之jmeter jdbc request 如何运行多个s ...

  8. Android启动过程_大致流程

    Android大致启动过程如图(基于O版本  使用draw.io画的). 注:这是通过查询结合自己了解的,还有不少不明确的,后续有进展完善,欢迎指正. 说明:绿色是主要几个的阶段.其他围绕这几个阶段的 ...

  9. springboot 启动报错"No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available"

    1.问题 springboot启动报错 "D:\Program Files\Java\jdk-11\bin\java.exe" -XX:TieredStopAtLevel=1 -n ...

  10. [Objective-C] 013_文件系统(File System)

    在前面三篇关于数据持久化,我们都用涉及到文件(plist文件,数据库文件),它们都是把它们存储在document目录下.iOS的文件机制是沙盒机制,应用只能访问自己应用目录下的文件.iOS应用产生的内 ...