Netty網絡聊天室及擴展序列化算法

一、前言

Netty是一個基于Java的高性能、事件驅動的網絡應用框架,廣泛應用于各種網絡通信場景。本文將介紹如何使用Netty構建一個簡單的網絡聊天室,并擴展序列化算法來提高數據傳輸效率和靈活性。

二、Netty網絡聊天室的實現

1. 項目結構

我們將使用Maven構建項目,項目結構如下:

netty-chatroom/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   ├── server/
│   │   │   │   ├── ChatServer.java
│   │   │   │   ├── ChatServerInitializer.java
│   │   │   │   ├── ChatServerHandler.java
│   │   │   ├── client/
│   │   │   │   ├── ChatClient.java
│   │   │   │   ├── ChatClientInitializer.java
│   │   │   │   ├── ChatClientHandler.java
│   │   ├── resources/
│   │       ├── log4j.properties
├── pom.xml
?

2. 服務器端實現

ChatServer.java
package server;import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;public class ChatServer {private final int port;public ChatServer(int port) {this.port = port;}public void start() throws Exception {NioEventLoopGroup bossGroup = new NioEventLoopGroup();NioEventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChatServerInitializer()).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);ChannelFuture f = b.bind(port).sync();f.channel().closeFuture().sync();} finally {workerGroup.shutdownGracefully();bossGroup.shutdownGracefully();}}public static void main(String[] args) throws Exception {new ChatServer(8080).start();}
}
?
ChatServerInitializer.java
package server;import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;public class ChatServerInitializer extends ChannelInitializer<SocketChannel> {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(new StringEncoder());ch.pipeline().addLast(new ChatServerHandler());}
}
?
ChatServerHandler.java
package server;import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;public class ChatServerHandler extends SimpleChannelInboundHandler<String> {private static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);@Overridepublic void handlerAdded(ChannelHandlerContext ctx) throws Exception {channels.add(ctx.channel());}@Overridepublic void handlerRemoved(ChannelHandlerContext ctx) throws Exception {channels.remove(ctx.channel());}@Overrideprotected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {for (var channel : channels) {if (channel != ctx.channel()) {channel.writeAndFlush("[Client] " + ctx.channel().remoteAddress() + " says: " + msg + "\n");} else {channel.writeAndFlush("[You] " + msg + "\n");}}}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close();}
}
?

3. 客戶端實現

ChatClient.java
package client;import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;public class ChatClient {private final String host;private final int port;public ChatClient(String host, int port) {this.host = host;this.port = port;}public void start() throws Exception {NioEventLoopGroup group = new NioEventLoopGroup();try {Bootstrap b = new Bootstrap();b.group(group).channel(NioSocketChannel.class).handler(new ChatClientInitializer());ChannelFuture f = b.connect(host, port).sync();f.channel().closeFuture().sync();} finally {group.shutdownGracefully();}}public static void main(String[] args) throws Exception {new ChatClient("localhost", 8080).start();}
}
?
ChatClientInitializer.java
package client;import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;public class ChatClientInitializer extends ChannelInitializer<SocketChannel> {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(new StringEncoder());ch.pipeline().addLast(new ChatClientHandler());}
}
?
ChatClientHandler.java
package client;import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;public class ChatClientHandler extends SimpleChannelInboundHandler<String> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {System.out.println(msg);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close();}
}
?

三、擴展序列化算法

為了提高數據傳輸效率,我們可以擴展Netty的序列化算法。Netty默認提供的序列化算法包括Java序列化、JSON、Protobuf等。下面介紹如何使用Protobuf進行序列化。

1. 配置Protobuf

首先,在?pom.xml中添加Protobuf依賴:

<dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><version>3.19.1</version>
</dependency>
?

2. 定義Protobuf消息

創建一個?chat.proto文件:

syntax = "proto3";package chat;message ChatMessage {string from = 1;string to = 2;string content = 3;
}
?

編譯Protobuf文件生成Java類:

protoc --java_out=src/main/java src/main/proto/chat.proto
?

3. 修改服務器端處理器

在服務器端,使用Protobuf進行消息的序列化和反序列化:

package server;import chat.ChatMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;public class ChatServerHandler extends SimpleChannelInboundHandler<ChatMessage> {private static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);@Overridepublic void handlerAdded(ChannelHandlerContext ctx) throws Exception {channels.add(ctx.channel());}@Overridepublic void handlerRemoved(ChannelHandlerContext ctx) throws Exception {channels.remove(ctx.channel());}@Overrideprotected void channelRead0(ChannelHandlerContext ctx, ChatMessage msg) throws Exception {for (var channel : channels) {if (channel != ctx.channel()) {channel.writeAndFlush(msg.toBuilder().setContent("[Client] " + ctx.channel().remoteAddress() + " says: " + msg.getContent()).build());} else {channel.writeAndFlush(msg.toBuilder().setContent("[You] " + msg.getContent()).build());}}}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close();}
}
?

4. 修改客戶端處理器

在客戶端,同樣使用Protobuf進行消息的序列化和反序列化:

package client;import chat.ChatMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;public class ChatClientHandler extends SimpleChannelInboundHandler<ChatMessage> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, ChatMessage msg) throws Exception {System.out.println(msg.getContent());}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close();}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/89899.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/89899.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/89899.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

基于單片機金沙河糧倉環境監測系統設計與實現

摘 要 本文圍繞基于單片機的金沙河糧倉環境監測系統展開設計與實現研究。系統以單片機為核心&#xff0c;集成 DHT11、MQ - 135 等傳感器&#xff0c;可實時精準監測糧倉溫濕度、氣體成分等關鍵環境參數。借助 LoRa、ESP8266 實現數據的可靠傳輸與遠程通信 &#xff0c;OLED 屏…

如何解決Android Studio安裝時無法下載SDK的問題(Windows、Linux、Mac解決方案大全)

如何解決Android Studio安裝時無法下載SDK的問題&#xff08;Windows、Linux、Mac解決方案大全&#xff09; 前言 對于全棧開發者而言&#xff0c;安裝 Android Studio 是邁向 Android 開發的第一步&#xff0c;但在 Windows、Linux、macOS 等不同平臺上&#xff0c;經常會遇…

SQL Server從入門到項目實踐(超值版)讀書筆記 21

9.5 數據的內連接查詢連接是關系數據庫模型的主要特點&#xff0c;連接查詢是關系數據庫中最主要的查詢&#xff0c;主要包括內連接、外連接等。內連接查詢操作列出與連接條件匹配的數據行&#xff0c;它使用比較運算符比較被鏈接列的列值。具體語法格式如下&#xff1a;SELECT…

瑞芯微7月17日舉辦開發者大會,多款AIoT新品發布,觸覺智能RK方案商報導

瑞芯微第九屆開發者大會RKDC 2025將有多款新品發布。 據瑞芯微電子Rockchip此前宣布&#xff1a;該企業的本年度開發者大會RKDC 2025將于7月17~18日在福建福州海峽國際會展中心舉行。本次瑞芯微開發者大會以“AIoT模型創新重做產品”為主題&#xff0c;關注傳統IoT功能設備向場…

Eureka+Ribbon實現服務注冊與發現

目錄 一、相關文章 二、兼容說明 三、服務注冊 四、服務發現 一、相關文章 基礎工程&#xff1a;gradle7.6.1springboot3.2.4創建微服務工程-CSDN博客 Eureka服務端啟動&#xff1a;https://blog.csdn.net/cherishSpring/article/details/149473554 Ribbon負載均衡&#…

數據庫、HTML

一、數據庫 數據庫文件與普通文件區別: 普通文件對數據管理(增刪改查)效率低2.數據庫對數據管理效率高&#xff0c;使用方便 常用數據庫: 1.關系型數據庫: 將復雜的數據結構簡化為二維表格形式 大型:0racle、DB2 中型:MySq1、sQLServer 小型:Sqlite 2.非關系型數據庫以鍵值對…

RCE隨筆(1)

哪些是可以執行代碼執行&#xff1a;php代碼。eval如&#xff1a;eval:<?php eval($_post[key]);eval&#xff1a;php中不被叫做函數叫動態執行命令assert&#xff1a;執行函數call_user_func_array<?php call_user_func_array(assert,array($_REQUEST[shu]));傳入xxs-…

FPGA——ZYNQ7020學習日記(PS端)4(開始PS控制VGA顯示)

1.DMA 我們的整體VGA顯示分為幾步&#xff1a;比如先導入VIDEO TIMING CONTROL來做對輸入數據的時序“對齊”&#xff0c;這里開源騷客寫的很詳細&#xff0c;先用了一個虛擬IO&#xff08;VIO)來作為輸入&#xff0c;導入了一個簡單的RTL模塊&#xff08;當VTL的使能信號有效…

AGX Xavier 搭建360環視教程【補充一:魚眼去畸變(Fisheye Undistortion)】

對每路幀做魚眼去畸變除了用cv::cuda::remap是否有更好的辦法呢&#xff1f;確實 cv::cuda::remap 不是唯一可選項&#xff0c;甚至未必是最高效或最適合實際業務量級的方案。&#x1f3af; 1?? 去畸變的原理魚眼相機&#xff08;或者大廣角相機&#xff09;會把直線拉彎&…

tomato靶機練習

下載完靶機后&#xff0c;直接運行&#xff0c;選擇安裝路徑后將虛擬機的網絡設置為nat模式&#xff0c;設置完成后重啟虛擬機掃描同一網段&#xff0c;查找主機&#xff0c;這里我們使用kali的nmap&#xff0c;既能找到主機&#xff0c;也能查看開啟的端口依次嘗試&#xff0c…

136. Java 泛型 - 下限通配符

文章目錄136. Java 泛型 - 下限通配符 (? super T)**1. 什么是下限通配符 (? super T)&#xff1f;****2. 為什么使用下限通配符&#xff1f;****3. 示例&#xff1a;使用 ? super Integer 允許添加 Integer****? 正確示例****4. 為什么 List<? super Integer> 和 L…

C++23中的std::expected:異常處理

C23中的std::expected:異常處理 眾所周知&#xff0c;C23以前的異常處理是比較麻煩的&#xff0c;尤其是自己要在可能拋出異常的地方&#xff0c;需要自己去捕獲它&#xff0c;比如除數為0的異常、使用std::stoi函數將字符串轉換成int整型數據、處理文件讀寫的異常等等&#x…

處理Electron Builder 創建新進程錯誤 spawn ENOMEM

這個錯誤 spawn ENOMEM 表明系統內存不足&#xff0c;無法為 Electron Builder 創建新進程。以下是一些可能的解決方案&#xff1a;釋放系統內存關閉不必要的程序和服務增加物理內存 (RAM) 或交換空間 (swap)使用 free -h 和 top 命令檢查內存使用情況臨時增加交換空間# 創建一…

discuz安裝使用教程,及網站部署在公網訪問

Discuz!的安裝主要包括環境準備、程序部署和配置管理三個核心步驟?&#xff0c;有條件 的可以使用寶塔面板或云鏡像簡化流程&#xff0c;本地部署無公網IP的配合類似nat123映射公網訪問&#xff0c;當前最新版本為Discuz! Q&#xff08;2025年發布&#xff09;和Discuz! X3.5&…

深入解析C#數字轉換:隱式與顯式轉換的全面指南

—— 數據精度保衛戰中的checked與unchecked秘籍 &#x1f4cc; 核心概念速覽 1. 隱式轉換 自動發生&#xff0c;無數據丟失風險&#xff08;如 int→long&#xff09;遵循"小類型→大類型"路徑&#xff08;見下圖??&#xff09; [圖1&#xff1a;C#隱式數字轉換路…

量子計算可以解決的三個現實問題

今年是量子力學被發現一百周年。這一突破幫助人們認識到&#xff0c;支配我們周圍世界最小層面&#xff08;分子、原子和亞原子粒子&#xff09;的物理定律&#xff0c;與支配我們在日常生活中與物體相互作用方式的物理定律有著根本的不同。量子力學讓我們能夠了解從血液中的新…

Valgrind Memcheck 全解析教程:6個程序說明基礎內存錯誤

Valgrind 是一個強大的動態分析框架&#xff0c;其中的 memcheck 工具用于檢測 C/C 程序中類型不定的內存錯誤&#xff0c;是基礎級內存調試工具的重要選擇。 本文將通過 6 段有意義的錯誤代碼&#xff0c;全面講解 memcheck 的檢測原理和輸出分析&#xff0c;進而幫助學習者托…

Vue3 實現 Excel 文件導入導出功能

在Vue 3中實現Excel文件的導入和導出功能&#xff0c;你可以使用一些流行的JavaScript庫&#xff0c;如SheetJS&#xff08;也稱為xlsx&#xff09;來處理Excel文件。以下是實現這一功能的基本步驟&#xff1a;1. 安裝SheetJS首先&#xff0c;你需要安裝xlsx庫。在你的Vue項目中…

CS231n-2017 Lecture2圖像分類筆記

圖像分類問題定義&#xff1a;在已有固定的分類標簽集合的前提下&#xff0c;能夠對輸入的圖像進行識別處理&#xff0c;從集合中找到該圖像所對應的標簽。對于計算機而言&#xff0c;圖像并非直觀的圖像&#xff0c;而是一個的像素集合&#xff0c;對于每個像素&#xff0c;其…

Typecho博客Ajax評論功能實現全攻略

文章目錄 Typecho實現Ajax評論功能的完整指南 引言 一、技術選型與準備工作 1.1 技術棧分析 1.2 環境準備 二、前端實現方案 2.1 基礎HTML結構 2.2 JavaScript處理邏輯 三、后端處理實現 3.1 創建插件處理Ajax請求 3.2 錯誤處理增強 四、安全性考慮 4.1 CSRF防護 4.2 輸入過濾 …