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

平湖有做网站得吗汕头网站排名优化

平湖有做网站得吗,汕头网站排名优化,安徽省卫生计生网站医共体建设,找外包做网站要多久1.Netty服务端 服务端代码参考【基于Netty实现安全认证的WebSocket(wss)服务端-CSDN博客】 2.Netty客户端 客户端代码参考【基于Netty实现WebSocket客户端-CSDN博客】中两种都可以;这里用的是第一种。 新增SslHandler的代码: …

1.Netty服务端

服务端代码参考【基于Netty实现安全认证的WebSocket(wss)服务端-CSDN博客】

2.Netty客户端

客户端代码参考【基于Netty实现WebSocket客户端-CSDN博客】中两种都可以;这里用的是第一种。

新增SslHandler的代码:

SslContext sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
...
pipeline.addLast(sslCtx.newHandler(sc.alloc(), webSocketURL.getHost(), webSocketURL.getPort()));

服务端地址的协议头调整为wss

final URI webSocketURL = new URI("wss://127.0.0.1:7070/helloWs");

完整的客户端代码如下:

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler;
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.handler.stream.ChunkedWriteHandler;
import lombok.extern.slf4j.Slf4j;import java.net.URI;
import java.util.concurrent.CountDownLatch;/*** https://blog.csdn.net/a1053765496/article/details/130701218* 基于Netty快速实现WebSocket客户端,不手动处理握手*/
@Slf4j
public class SimpleWssClient {final CountDownLatch latch = new CountDownLatch(1);public static void main(String[] args) throws Exception {SimpleWssClient client = new SimpleWssClient();client.test();}public void test() throws Exception {Channel dest = dest();latch.await();dest.writeAndFlush(new TextWebSocketFrame("CountDownLatch完成后发送的消息"));}public Channel dest() throws Exception {final URI webSocketURL = new URI("wss://127.0.0.1:7070/helloWs");EventLoopGroup group = new NioEventLoopGroup();SslContext sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();Bootstrap boot = new Bootstrap();boot.option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.TCP_NODELAY, true).group(group).handler(new LoggingHandler(LogLevel.INFO)).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel sc) throws Exception {ChannelPipeline pipeline = sc.pipeline();pipeline.addLast(sslCtx.newHandler(sc.alloc(), webSocketURL.getHost(), webSocketURL.getPort()));pipeline.addLast(new HttpClientCodec());pipeline.addLast(new ChunkedWriteHandler());pipeline.addLast(new HttpObjectAggregator(64 * 1024));pipeline.addLast(new WebSocketClientProtocolHandler(WebSocketClientHandshakerFactory.newHandshaker(webSocketURL, WebSocketVersion.V13, null, false, new DefaultHttpHeaders())));pipeline.addLast(new SimpleChannelInboundHandler<TextWebSocketFrame>() {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg)throws Exception {System.err.println(" 客户端收到消息======== " + msg.text());}@Overridepublic void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {if (WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE.equals(evt)) {log.info(ctx.channel().id().asShortText() + " 握手完成!");latch.countDown();send(ctx.channel());}super.userEventTriggered(ctx, evt);}});}});ChannelFuture cf = boot.connect(webSocketURL.getHost(), webSocketURL.getPort()).sync();return cf.channel();}public static void send(Channel channel) {final String textMsg = "握手完成后直接发送的消息";if (channel != null && channel.isActive()) {TextWebSocketFrame frame = new TextWebSocketFrame(textMsg);channel.writeAndFlush(frame).addListener((ChannelFutureListener) channelFuture -> {if (channelFuture.isDone() && channelFuture.isSuccess()) {log.info("     ================= 发送成功.");} else {channelFuture.channel().close();log.info("     ================= 发送失败. cause = " + channelFuture.cause());channelFuture.cause().printStackTrace();}});} else {log.error("消息发送失败! textMsg = " + textMsg);}}}

参考:利用netty开发webScoketClient(支持wss协议,客户端、服务端心跳实现)_websocketclient-CSDN博客

http://www.tj-hxxt.cn/news/13724.html

相关文章:

  • node.js做企业网站seo课程排行榜
  • 平罗门户网站建设网络优化公司哪家好
  • 加强网站政务服务建设中国最新军事新闻
  • 工程机械 网站模板百度竞价登陆
  • 昔阳网站建设成都百度推广账户优化
  • 网页动态设计优化关键词排名软件
  • 关于做公司官方网站域名申请百度seo搜索营销新视角
  • 提供网站建设公司哪家好十堰seo
  • 南昌企业网站排名优化全网营销推广方案外包
  • 花生壳做局域网站关键词优化排名平台
  • 东莞企石网站建设免费建网站平台
  • 网站开发准备流程图如何用百度平台营销
  • 胶州网站制作百度全网营销
  • 网站上的洗衣液瓶子做花瓶怎么材质杭州seo博客
  • 高端的网站开发公司谷歌seo代运营
  • 手机网站改版外链发布工具
  • 如何确定wordpress赣州seo唐三
  • 阜宁做网站的公司电话搜关键词网站
  • 淘宝客做网站推广网络运营工作内容
  • 做护肤的网站有哪些免费网站申请注册
  • 徐州网站定制自建网站平台有哪些
  • 西安网站建设设计的好公司哪家好短视频运营
  • 学校网站推广手机系统流畅神器
  • 四川住房建设部网站关键词异地排名查询
  • 株洲论坛seo优化自动点击软件
  • 企业门户网站开发优化营商环境个人心得
  • 一般网站banner尺寸公众号运营收费价格表
  • 清爽帝国cmsseo推广服务
  • H5网站开发工程师seo小白入门
  • 网络营销网站建设论文免费友情链接网