当前位置: 首页 > news >正文

工作室网站模板离我最近的电脑培训中心

工作室网站模板,离我最近的电脑培训中心,石家庄最新新闻,中企动力做网站费用断连重试有以下两点考虑: 1、连接异常,比如网络抖动导致连接失败; 2、连接过程中断开连接重试; 主要用到两个工具类: ChannelFutureListener监听ChannelFuture..isSuccess(); ChannelInboundHandlerAd…

断连重试有以下两点考虑:

1、连接异常,比如网络抖动导致连接失败;

2、连接过程中断开连接重试;

主要用到两个工具类:

ChannelFutureListener监听ChannelFuture..isSuccess();

ChannelInboundHandlerAdapter重写channelInactive,当连接变为不活跃,则回调该方法。

完整代码如下:

@Component
public class WebSocketClient {private Channel channel;private Bootstrap bootstrap;private URI uri;private MessageHandler messageHandler;private WebSocketClientHandler handler;private volatile AtomicInteger atomicCount = new AtomicInteger(0);public WebSocketClient initClient(String host, MessageHandler messageHandler) throws Exception {this.messageHandler = messageHandler;if (StringUtils.isEmpty(host)) {throw new RuntimeException("未配置host.");}uri = new URI(host);String scheme = uri.getScheme() == null? WssSchemeEnum.WS.getValue() : uri.getScheme();//判断是否ssl连接,如果是则设置为可信final boolean ssl = WssSchemeEnum.WSS.getValue().equalsIgnoreCase(scheme);final SslContext sslCtx;if (ssl) {sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();} else {sslCtx = null;}EventLoopGroup group = new NioEventLoopGroup();try {bootstrap = new Bootstrap();bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) {ChannelPipeline p = ch.pipeline();p.addFirst(new ChannelInboundHandlerAdapter() {@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {log.error("【{}】检测到wss断连, 第 {} 次发起重连.", exchange, atomicCount.incrementAndGet());super.channelInactive(ctx);ctx.channel().eventLoop().schedule(WebSocketClient.this::doConnect, 3000, TimeUnit.MILLISECONDS);}});if (sslCtx != null) {p.addLast(sslCtx.newHandler(ch.alloc(), uri.getHost(), getUriPort(uri)));}p.addLast(new HttpClientCodec());p.addLast(new HttpObjectAggregator(8192));p.addLast(WebSocketClientCompressionHandler.INSTANCE);handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()), exchange, messageHandler);p.addLast(handler);}});} catch (Exception e) {log.error("wss创建client异常. e:", e);if (bootstrap != null) {bootstrap.config().group().shutdownGracefully();}throw new RuntimeException("初始化wss连接异常. e: " + e);}doConnect();return this;}public void doConnect() {try {ChannelFuture future = bootstrap.connect(uri.getHost(), getUriPort(uri)).sync();handler.handshakeFuture().sync();future.addListener((ChannelFutureListener) cf -> {if (future.isSuccess()) {channel = future.channel();WssManger.addChannel(exchange, channel);log.info("连接成功.");messageHandler.connectSuccessAction(future.channel());atomicCount.set(0);} else {log.error("监听断连, wss第 {} 次发起重连. ", atomicCount.incrementAndGet());future.channel().eventLoop().schedule(WebSocketClient.this::doConnect, 3000, TimeUnit.MILLISECONDS);}});}catch (Exception e) {log.error("连接异常. e:" + e);if (bootstrap != null) {log.info("wss连接异常,第 {} 次发起重连.", atomicCount.incrementAndGet());bootstrap.config().group().schedule(WebSocketClient.this::doConnect, 3000, TimeUnit.MILLISECONDS);}}}/*** 根据URI获取对应的port** @param uri uri* @return port*/private int getUriPort(URI uri) {String scheme = uri.getScheme() == null? WssSchemeEnum.WS.getValue() : uri.getScheme();if (!WssSchemeEnum.allScheme().contains(scheme)) {throw new RuntimeException("Only WS(S) is supported.");}if (uri.getPort() == -1) {if (WssSchemeEnum.WS.getValue().equalsIgnoreCase(scheme)) {return WssSchemeEnum.WS.getPort();} else if (WssSchemeEnum.WSS.getValue().equalsIgnoreCase(scheme)) {return WssSchemeEnum.WSS.getPort();} else {return -1;}} else {return uri.getPort();}}
}

http://www.hrbkazy.com/news/3743.html

相关文章:

  • 做网站如何做视频天津seo培训机构
  • 徐州企业网站建设公司企业产品网络推广
  • 宁波市鄞州区建设局网站百度网址提交入口平台
  • wordpress文章点击数插件网站优化排名易下拉排名
  • 网站建设百度文库今天重大新闻头条新闻军事
  • 吉林省住房与城乡建设厅网站近日发生的重大新闻
  • 柳市网站托管广告投放怎么做
  • facebook 网站开发南京最大网站建设公司
  • 免费网站建设企业长春网站快速排名提升
  • 阿里巴巴网站怎么做才能排第一合肥seo排名优化
  • 小说网站做编辑器新品牌推广策略
  • html css设计与构建网站nba东西部最新排名
  • 微信恢复官方网站百度seo高级优化
  • 做网站全包行业关键词
  • 网约车平台app网站建设重庆网站优化排名推广
  • 怎么直接做免费网站吗品牌公关
  • 做汽车租赁主要的网站搜索引擎优化员简历
  • 网站建设web标准西安百度推广运营公司
  • 网站跟域名备案数量专门培训seo的网站
  • 南京做网站哪家公司最好百度关键词搜索量排名
  • 微信网站建设哪家好网站推广优化的公司
  • 一个网站成本小红书代运营
  • 低价做网站合肥百度关键词排名
  • 浙江人工智能建站系统软件建网站的详细步骤
  • 如何做淘宝的站外网站推广seo实战培训课程
  • win7网站服务器制作软件北京seoqq群
  • 北湖区网站建设哪个好知了seo
  • 医院关于建设官方网站的请示含有友情链接的网页
  • b2c电子商务购物网站有哪些福州短视频seo方法
  • 网站添加背影音乐怎么做报个计算机培训班多少钱