mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-22 22:26:06 +00:00
perf[ws]: add IdleStateHandler to websocket server
This commit is contained in:
@@ -17,6 +17,7 @@ import com.zfoo.net.core.AbstractClient;
|
||||
import com.zfoo.net.core.HostAndPort;
|
||||
import com.zfoo.net.handler.ClientRouteHandler;
|
||||
import com.zfoo.net.handler.codec.json.JsonWebSocketCodecHandler;
|
||||
import com.zfoo.net.handler.idle.ClientIdleHandler;
|
||||
import com.zfoo.protocol.util.IOUtils;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.codec.http.HttpClientCodec;
|
||||
@@ -24,6 +25,7 @@ import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler;
|
||||
import io.netty.handler.stream.ChunkedWriteHandler;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
@@ -39,6 +41,8 @@ public class JsonWebsocketClient extends AbstractClient<SocketChannel> {
|
||||
|
||||
@Override
|
||||
public void initChannel(SocketChannel channel) {
|
||||
channel.pipeline().addLast(new IdleStateHandler(0, 0, 60));
|
||||
channel.pipeline().addLast(new ClientIdleHandler());
|
||||
channel.pipeline().addLast(new HttpClientCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
|
||||
channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB));
|
||||
channel.pipeline().addLast(new WebSocketClientProtocolHandler(webSocketClientProtocolConfig));
|
||||
|
||||
@@ -17,12 +17,14 @@ import com.zfoo.net.core.AbstractServer;
|
||||
import com.zfoo.net.core.HostAndPort;
|
||||
import com.zfoo.net.handler.ServerRouteHandler;
|
||||
import com.zfoo.net.handler.codec.json.JsonWebSocketCodecHandler;
|
||||
import com.zfoo.net.handler.idle.ServerIdleHandler;
|
||||
import com.zfoo.protocol.util.IOUtils;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
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.stream.ChunkedWriteHandler;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
@@ -35,6 +37,8 @@ public class JsonWebsocketServer extends AbstractServer<SocketChannel> {
|
||||
|
||||
@Override
|
||||
public void initChannel(SocketChannel channel) {
|
||||
channel.pipeline().addLast(new IdleStateHandler(0, 0, 180));
|
||||
channel.pipeline().addLast(new ServerIdleHandler());
|
||||
// 编解码 http 请求
|
||||
channel.pipeline().addLast(new HttpServerCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
|
||||
// 聚合解码 HttpRequest/HttpContent/LastHttpContent 到 FullHttpRequest
|
||||
|
||||
@@ -17,6 +17,7 @@ import com.zfoo.net.core.AbstractClient;
|
||||
import com.zfoo.net.core.HostAndPort;
|
||||
import com.zfoo.net.handler.ClientRouteHandler;
|
||||
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
|
||||
import com.zfoo.net.handler.idle.ClientIdleHandler;
|
||||
import com.zfoo.protocol.util.IOUtils;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.codec.http.HttpClientCodec;
|
||||
@@ -24,6 +25,7 @@ import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler;
|
||||
import io.netty.handler.stream.ChunkedWriteHandler;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
|
||||
|
||||
/**
|
||||
@@ -40,6 +42,8 @@ public class WebsocketClient extends AbstractClient<SocketChannel> {
|
||||
|
||||
@Override
|
||||
public void initChannel(SocketChannel channel) {
|
||||
channel.pipeline().addLast(new IdleStateHandler(0, 0, 60));
|
||||
channel.pipeline().addLast(new ClientIdleHandler());
|
||||
channel.pipeline().addLast(new HttpClientCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
|
||||
channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB));
|
||||
channel.pipeline().addLast(new WebSocketClientProtocolHandler(webSocketClientProtocolConfig));
|
||||
|
||||
@@ -17,12 +17,14 @@ import com.zfoo.net.core.AbstractServer;
|
||||
import com.zfoo.net.core.HostAndPort;
|
||||
import com.zfoo.net.handler.ServerRouteHandler;
|
||||
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
|
||||
import com.zfoo.net.handler.idle.ServerIdleHandler;
|
||||
import com.zfoo.protocol.util.IOUtils;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
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.stream.ChunkedWriteHandler;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
@@ -35,6 +37,8 @@ public class WebsocketServer extends AbstractServer<SocketChannel> {
|
||||
|
||||
@Override
|
||||
public void initChannel(SocketChannel channel) {
|
||||
channel.pipeline().addLast(new IdleStateHandler(0, 0, 180));
|
||||
channel.pipeline().addLast(new ServerIdleHandler());
|
||||
// 编解码 http 请求
|
||||
channel.pipeline().addLast(new HttpServerCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
|
||||
// 聚合解码 HttpRequest/HttpContent/LastHttpContent 到 FullHttpRequest
|
||||
|
||||
Reference in New Issue
Block a user