From 5091e0fa6a538bfd99e2874adc2fd56c3d2b9a25 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 3 May 2018 22:23:33 +0900 Subject: [PATCH] ing --- .../server/websocket/client/Client.java | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/main/java/com/loafle/commons/server/websocket/client/Client.java diff --git a/src/main/java/com/loafle/commons/server/websocket/client/Client.java b/src/main/java/com/loafle/commons/server/websocket/client/Client.java new file mode 100644 index 0000000..2b972dc --- /dev/null +++ b/src/main/java/com/loafle/commons/server/websocket/client/Client.java @@ -0,0 +1,104 @@ +package com.loafle.commons.server.websocket.client; + +import java.net.SocketAddress; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandler; +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.SocketChannel; +import io.netty.handler.codec.http.HttpClientCodec; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker; +import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler; +import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler; + +/** + * Client + */ +public class Client { + private static final Logger logger = LoggerFactory.getLogger(Client.class); + public static final String CHANNEL_CLASS = "CLIENT_CHANNEL_CLASS"; + public static final String CHANNEL_HANDLER = "CLIENT_CHANNEL_HANDLER"; + public static final String HANDSHAKER = "CLIENT_HANDSHAKER"; + public static final String SOCKET_ADDRESS = "CLIENT_SOCKET_ADDRESS"; + + @Autowired + @Qualifier(CHANNEL_CLASS) + protected Class channelClass; + @Autowired + @Qualifier(CHANNEL_HANDLER) + protected ChannelHandler handler; + @Autowired + @Qualifier(HANDSHAKER) + WebSocketClientHandshaker handshaker; + @Autowired + @Qualifier(SOCKET_ADDRESS) + protected SocketAddress address; + + private Channel channel; + + public void start() throws Exception { + EventLoopGroup group = new NioEventLoopGroup(); + try { + Bootstrap b = new Bootstrap(); + b.group(group); + if (null != this.channelClass) { + b = b.channel(this.channelClass); + } + b.handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) { + ChannelPipeline p = ch.pipeline(); + p.addLast( + new HttpClientCodec(), + new HttpObjectAggregator(8192), + WebSocketClientCompressionHandler.INSTANCE, + new WebSocketClientProtocolHandler(handshaker, true), + handler + ); + } + }); + + this.init(); + ChannelFuture cf = b.connect(this.address); + this.onStart(); + cf.sync(); + + this.channel = cf.channel(); + } catch (Exception e) { + logger.error("Client", e); + } finally { + this.onStop(); + group.shutdownGracefully(); + this.destroy(); + } + } + + public void stop() throws Exception { + this.channel.close().sync(); + } + + protected void init() throws Exception { + // no op + } + protected void onStart() throws Exception { + // no op + } + protected void onStop() throws Exception { + // no op + } + protected void destroy() throws Exception { + // no op + } + +} \ No newline at end of file