新写法之服务端(java netty)基于prtobuf多协议websocket信息处理 (非官方写法)

本文介绍了作者在学习Netty过程中,如何基于Protobuf实现多协议WebSocket信息处理的非官方写法。作者批评了官方对Protobuf多协议支持的不足,并分享了自己创建的服务端启动、数据接收与解读、数据封装与发送的详细步骤,展示了如何将protobuf转换为BinaryWebSocketFrame进行传输。
摘要由CSDN通过智能技术生成

刚接触netty,就被他深深吸引.经过4天的不断学习和研究,终于被我跑通了,以下我就分享给大家!

首先我吐槽以下他官方对protobuf多协议那种堪比shit的支持,导致我4天3天都在坑里摸爬滚打!

以下是我自创的,支持多协议,又不失优雅的写法,目前网上找不到的! 不多说,走你

首先是服务端的启动:

package com.zhcj.netty;

import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * 
 * @author Dan
 * 2020-07-04
 *
 */
@Service
public class NettyServer {
	public static void main(String[] args) {
		new NettyServer().run();
	}

	@PostConstruct
	public void initNetty() {
		new Thread() {
			public void run() {
				new NettyServer().run();
			}
		}.start();
	}

	public void run() {
		System.out.println("===========================Netty端口启动===========&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Artemis丶月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值