feat: adding handshake connection handler

feat: finish connection handler
This commit is contained in:
wzp 2024-07-31 03:59:19 +08:00
parent 2c39144ece
commit 9be2861921
5 changed files with 47 additions and 20 deletions

View File

@ -37,6 +37,6 @@ public class Main {
WebSocketConnectionHandler webSocketConnectionHandler = new WebSocketConnectionHandler();
ChannelFuture future = webSocketConnectionHandler.connect(uri);
Channel channel = future.sync().channel();
log.info("连接服务器成功!");
}
}

View File

@ -0,0 +1,35 @@
package cn.wzpmc.network;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
/**
* 握手包处理器
* @author wzp
* @version 0.0.1-dev
* @since 2024/7/31 上午3:53
*/
@Log4j2
@RequiredArgsConstructor
public class HandshakePacketHandler extends SimpleChannelInboundHandler<FullHttpResponse> {
private final WebSocketClientHandshaker handshaker;
@Override
public void channelActive(ChannelHandlerContext ctx) {
log.debug("开始WebSocket握手");
this.handshaker.handshake(ctx.channel());
log.debug("发送握手包");
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpResponse fullHttpResponse) {
if (!handshaker.isHandshakeComplete()) {
handshaker.finishHandshake(channelHandlerContext.channel(), fullHttpResponse);
log.debug("握手成功");
log.info("连接服务器成功!");
}
}
}

View File

@ -2,30 +2,20 @@ package cn.wzpmc.network;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import lombok.RequiredArgsConstructor;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.log4j.Log4j2;
/**
* websocket包处理器
* @author wzp
* @version 0.0.1-dev
* @since 2024/7/31 上午12:14
*/
@Log4j2
@RequiredArgsConstructor
public class PacketHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
private final WebSocketClientHandshaker handshaker;
public class PacketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
log.debug("开始WebSocket握手");
this.handshaker.handshake(ctx.channel());
log.debug("握手完成");
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, WebSocketFrame webSocketFrame) throws Exception {
System.out.println(webSocketFrame);
protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame webSocketFrame) {
log.info(webSocketFrame.text());
}
}

View File

@ -18,12 +18,14 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class WebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {
private final ChannelHandler handler;
private final HandshakePacketHandler handshakePacketHandler;
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
protected void initChannel(SocketChannel socketChannel) {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(new HttpObjectAggregator(64 * 1024));
pipeline.addLast(handshakePacketHandler);
pipeline.addLast(handler);
}
}

View File

@ -12,7 +12,6 @@ import io.netty.handler.codec.http.websocketx.WebSocketVersion;
import lombok.extern.log4j.Log4j2;
import java.net.URI;
import java.util.concurrent.Future;
/**
* 此类用于建立WebSocket连接
@ -33,8 +32,9 @@ public class WebSocketConnectionHandler {
log.info("正在连接websocket");
Bootstrap bootstrap = new Bootstrap();
WebSocketClientHandshaker clientHandshaker = WebSocketClientHandshakerFactory.newHandshaker(websocket, WebSocketVersion.V13, null, false, new DefaultHttpHeaders());
PacketHandler handler = new PacketHandler(clientHandshaker);
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new WebSocketChannelInitializer(handler));
HandshakePacketHandler handshakePacketHandler = new HandshakePacketHandler(clientHandshaker);
PacketHandler handler = new PacketHandler();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new WebSocketChannelInitializer(handler, handshakePacketHandler));
return bootstrap.connect(websocket.getHost(), websocket.getPort());
}