您的位置:首页 > 健康 > 美食 > 营销网站建设培训_如何创建网站的步骤_最新资讯热点_网络软文营销案例3篇

营销网站建设培训_如何创建网站的步骤_最新资讯热点_网络软文营销案例3篇

2024/12/24 0:06:11 来源:https://blog.csdn.net/qq_37280992/article/details/144454561  浏览:    关键词:营销网站建设培训_如何创建网站的步骤_最新资讯热点_网络软文营销案例3篇
营销网站建设培训_如何创建网站的步骤_最新资讯热点_网络软文营销案例3篇
package cn.rainwer.nettywebsocket.Server;import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpServerExpectContinueHandler;public class NettyHttpServer {public static void main(String[] args) {EventLoopGroup bossGroup = new NioEventLoopGroup(1);EventLoopGroup workerGroup = new NioEventLoopGroup();ChannelFuture f = null;try {//ServerBootstrap负责初始化netty服务器,并且开始监听端口的socket请求ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {// 为监听客户端read/write事件的Channel添加用户自定义的ChannelHandlersocketChannel.pipeline()
//                                    .addLast("decoder", new HttpRequestDecoder())
//                                    .addLast("encoder", new HttpResponseEncoder()).addLast(new HttpServerCodec()).addLast(new HttpServerExpectContinueHandler()).addLast("aggregator", new HttpObjectAggregator(512 * 1024)).addLast(new NettyWebSocketConnectHandler2());}});f = b.bind(9996).sync();System.out.println("======NettyServer启动成功!!!=========");// f.channel().closeFuture().sync();} catch (Exception e) {e.printStackTrace();} finally {if (f != null && f.isSuccess()) {System.out.println("Netty server listening on port " + " and ready for connections...");} else {System.out.println("Netty server start up Error!");}}}
}
package cn.rainwer.nettywebsocket.Server;import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import java.net.InetAddress;
import java.nio.charset.StandardCharsets;@Slf4j
@ChannelHandler.Sharable
@Component
public class NettyWebSocketConnectHandler2 extends SimpleChannelInboundHandler<Object> {@Overridepublic void channelRead(ChannelHandlerContext channelHandlerContext, Object msg) throws Exception {if (msg instanceof FullHttpRequest) {log.info("准备提取token");//转化为http请求FullHttpRequest request = (FullHttpRequest) msg;//拿到请求地址String uri = request.uri();System.out.println(uri);//判断是不是websocket请求,如果是拿出我们传递的参数(我的是token)channelHandlerContext.channel().writeAndFlush("Welcome to " + InetAddress.getLocalHost().getHostName() + " service!\n");FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.OK, Unpooled.wrappedBuffer(uri.getBytes(StandardCharsets.UTF_8)));fullHttpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN).set(HttpHeaderNames.CONTENT_LENGTH, fullHttpResponse.content().readableBytes());channelHandlerContext.writeAndFlush(fullHttpResponse);} else {}}
}

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com