ing
This commit is contained in:
parent
9677c63de9
commit
5091e0fa6a
|
@ -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<? extends Channel> 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<SocketChannel>() {
|
||||
@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
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user