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

网站建设询价函成都网站建设公

网站建设询价函,成都网站建设公,网站备案 厦门,为企业做网站的公司功能介绍Netty开发服务器HTML实现客户端实现服务端与客户端时实时交互开发步骤1.导包io.nettynetty‐all5.0.0.Alpha22.工程配置文件#xff1a;NettyConfig/*** 这里放的是工程中相应的配置*/public class NettyConfig{/*** 用于存储每一个客户端接入进来时的channel对象*/pu…功能介绍Netty开发服务器HTML实现客户端实现服务端与客户端时实时交互开发步骤1.导包io.nettynetty‐all5.0.0.Alpha22.工程配置文件NettyConfig/*** 这里放的是工程中相应的配置*/public class NettyConfig{/*** 用于存储每一个客户端接入进来时的channel对象*/public static ChannelGroup group new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);}3.MyWebSocketHandlerpublic class MyWebSocketHandler extends SimpleChannelInboundHandler { private WebSocketServerHandshaker handshaker;//请求路径private static final String WEB_SOCKET_URL ws://localhost:8888/websocket;//工程出现异常的时候调用Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace(); ctx.close(); //关闭当前连接}//客户端与服务器创建连接的时候调用Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception { NettyConfig.group.add(ctx.channel());System.out.println(客户端与服务器连接开启);}//客户端与服务器断开连接的时候调用Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception { NettyConfig.group.remove(ctx.channel());System.out.println(客户端与服务器断开连接);}//服务端接收客户端发送过来的数据结束之后调用Overridepublic void channelReadComplete(ChannelHandlerContext ctx) throws Exception{ ctx.flush(); //进行数据清除}//服务端处理客户端调用的核心方法protected void messageReceived(ChannelHandlerContext context, Object msg) throws Exception {//处理客户端向服务端发起http握手请求业务if(msg instanceof FullHttpRequest){handHttpRequest(context,(FullHttpRequest) msg);}else if(msg instanceof WebSocketFrame){//处理websocket连接业务handWebSocketFrme(context,(WebSocketFrame)msg);}}/*** 处理客户端与服务端之前的websocket业务* param ctx* param frame*/private void handWebSocketFrme(ChannelHandlerContext ctx,WebSocketFrame frame){//判断是否是关闭websocket的指令if(frame instanceof CloseWebSocketFrame){ handshaker.close(ctx.channel(),((CloseWebSocketFrame) frame).retain());}//判断是否是Ping消息if(frame instanceof PingWebSocketFrame){//返回一个胖信息ctx.channel().write(newPongWebSocketFrame(frame.content().retain())); return;}//判断是否是二进制消息如果是二进制消息抛出异常if(!(frame instanceof TextWebSocketFrame)){System.out.println(目前我们不支持二进制消息);throw new RuntimeException(this.getClass().getName()不支持的消息);}//返回应答消息//获取客户端向服务端发送的消息String request ((TextWebSocketFrame)frame).text(); System.out.println(服务端收到客户端的消息request); TextWebSocketFrame tws new TextWebSocketFrame(newDate().toString()ctx.channel().id()request);//群发服务端向每个上来的客户端群发消息NettyConfig.group.writeAndFlush(tws);}/*** 处理客户端向服务器发起http握手请求的业务* param ctx* param req*/private void handHttpRequest(ChannelHandlerContext ctx,FullHttpRequest req){// req.headers().get()// req.decoderResult().isSuccess()//不是websocket,就不是客户端发给服务器的Http请求if(!req.decoderResult().isSuccess()|| !(websocket).equals(req.headers().get(Upgrade))){sendHttpResponse(ctx,req,new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));return;}WebSocketServerHandshakerFactory wsFactory newWebSocketServerHandshakerFactory(WEB_SOCKET_URL,null,false); handshaker wsFactory.newHandshaker(req);if(handshakernull){WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());}else{handshaker.handshake(ctx.channel(),req);}}/*** 服务端向客户端响应消息* param ctx* param req* param res*/private void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, DefaultFullHttpResponse res){if(res.status().code()!200){ByteBuf buf Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);res.content().writeBytes(buf); buf.release();}//服务器向客户端发送数据ChannelFuture f ctx.channel().writeAndFlush(res); if(res.status().code()!200){f.addListener(ChannelFutureListener.CLOSE);}}}4 初始化连接各个组件:MyWebSocketChannelHandler/*** 初始化连接各个组件*/public class MyWebSocketChannelHandlerextends ChannelInitializer{protected void initChannel(SocketChannel e) throws Exception { e.pipeline().addLast(http‐codec,new HttpServerCodec()); e.pipeline().addLast(aggregator,new HttpObjectAggregator(66636)); e.pipeline().addLast(http‐chunked,new ChunkedWriteHandler()); e.pipeline().addLast(handler,new MyWebSocketHandler());}}5 程序入口/*** 程序的入口负责启动应用*/public class Main {public static void main(String[] args) { EventLoopGroup bossGroup new NioEventLoopGroup(); EventLoopGroup workGroup new NioEventLoopGroup();try{ServerBootstrap b new ServerBootstrap(); b.group(bossGroup,workGroup); b.channel(NioServerSocketChannel.class); b.childHandler(new MyWebSocketChannelHandler());System.out.println(服务端开启等待客户端连接);Channel ch b.bind(8888).sync().channel(); ch.closeFuture().sync();} catch (Exception e) {e.printStackTrace();} finally {//优雅的退出程序bossGroup.shutdownGracefully(); workGroup.shutdownGracefully();}}}6 web端测试代码WebSocket客户端var socket; if(!window.WebSocket){window.WebSocket window.MozWebSocket;}if(window.WebSocket){socket new WebSocket(ws://localhost:8888/websocket); socket.onmessage function (event) {var ta document.getElementById(responseContext); ta.value event.data \r\n;}socket.onopen function (event) {var ta document.getElementById(responseContext); ta.value 您浏览器支持webSoceket请进行后续 操作\r\n;}socket.onclose function (event) {var ta document.getElementById(responseContext); ta.value ;ta.value webSoceket连接已经关闭;}}else{console.debug(浏览器不支持webSocket)}function send() {var message document.getElementById(message).value; if(!window.WebSocket){return;}if(socket.readyState WebSocket.OPEN){ socket.send(message)}else{alert(连接没有建立成功);}}客户端接收到服务端返回的消息
http://www.zqtcl.cn/news/801022/

相关文章:

  • 新网站建设特色网站建设信息表
  • 商城做网站家具网站模板
  • 国有企业网站建设网站悬浮qq
  • 上海建站宝盒微网站生成app
  • 做网站是什么时候分页有哪些制作网站的公司
  • 专业柳州网站建设哪家好5千ip的网站能赚多少钱
  • 网站开发代理最火网页游戏
  • 做网站运营工资多少网站建设协议需要注意的问题
  • 如何建设一个人工智能网站qq头像网站源码
  • 有什么网站可以做外贸出口信息泉州网站制作运营商专业
  • 创业seo快速排名优化公司
  • 安丘网站开发王野天 女演员
  • 沈阳软件公司 网站制作wordpress未验证邮箱用户
  • 做动画上传网站赚钱么杭州市网站建设公司
  • 网站建设注意细节问题微信二维码
  • 凡科做的网站提示证书错误网络营销渠道可分为哪几种
  • 南京手机网站制作公司免费设计房屋效果图软件有哪些
  • 定制类网站怎么样做网页设计
  • 企业门户网站建设优势网站登录模版
  • 六盘水建设网站徐州建站平台
  • 昆明有多少做网站的公司公司软文代写
  • 东莞模板网站做一个电子商务网站在哪里做
  • 给别人网站做跳转株洲专业网站排名优化
  • 国外网站空间租用费用网站前端设计图
  • 宜州做网站点点网 xml转wordpress
  • 太原建站方法erp系统好上手吗
  • 网站建设如何实现检索功能河南城乡建设网站
  • 江苏做电缆桥架的公司网站购物网站答辩ppt怎么做
  • 惠州网站建设系统公司公司网站建设公司
  • 做酒类直供网站行吗石家庄桥西网站制作公司