乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      基于Netty5.0高級(jí)案例一之NettyWebsocket

       hmtomyang 2017-04-15

      基于Netty5.0高級(jí)案例一之NettyWebsocket

      2015-1-1 付政委 Netty5.0 高級(jí)篇

      前言介紹:

          本案例主要介紹如何使用Netty開發(fā)websocket。

      環(huán)境需求:

          1、jdk1.7

          2、Eclipse

          3、Netty5.0

          4、支持websocket的瀏覽器[火狐]
      工程截圖:


      代碼部分:


      服務(wù)端:

      Global.Java

      1. package com.itstack.netty.common;

      2. import io.netty.channel.group.ChannelGroup;
      3. import io.netty.channel.group.DefaultChannelGroup;
      4. import io.netty.util.concurrent.GlobalEventExecutor;

      5. public class Global {

      6. public static ChannelGroup group = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
      7. }



      ChildChannelHandler.java

      1. package com.itstack.netty.websocket;

      2. import io.netty.channel.ChannelInitializer;
      3. import io.netty.channel.socket.SocketChannel;
      4. import io.netty.handler.codec.http.HttpObjectAggregator;
      5. import io.netty.handler.codec.http.HttpServerCodec;
      6. import io.netty.handler.stream.ChunkedWriteHandler;

      7. public class ChildChannelHandler extends ChannelInitializer<SocketChannel>{

      8. @Override
      9. protected void initChannel(SocketChannel e) throws Exception {
      10. e.pipeline().addLast("http-codec",new HttpServerCodec());
      11. e.pipeline().addLast("aggregator",new HttpObjectAggregator(65536));
      12. e.pipeline().addLast("http-chunked",new ChunkedWriteHandler());
      13. e.pipeline().addLast("handler",new MyWebSocketServerHandler());
      14. }

      15. }



      MyWebSocketServerHandler.java


      1. package com.itstack.netty.websocket;

      2. import java.util.Date;
      3. import java.util.logging.Level;
      4. import java.util.logging.Logger;

      5. import com.itstack.netty.common.Global;

      6. import io.netty.buffer.ByteBuf;
      7. import io.netty.buffer.Unpooled;
      8. import io.netty.channel.ChannelFuture;
      9. import io.netty.channel.ChannelFutureListener;
      10. import io.netty.channel.ChannelHandlerContext;
      11. import io.netty.channel.SimpleChannelInboundHandler;
      12. import io.netty.handler.codec.http.DefaultFullHttpRequest;
      13. import io.netty.handler.codec.http.DefaultFullHttpResponse;
      14. import io.netty.handler.codec.http.FullHttpRequest;
      15. import io.netty.handler.codec.http.HttpResponseStatus;
      16. import io.netty.handler.codec.http.HttpVersion;
      17. import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
      18. import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
      19. import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
      20. import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
      21. import io.netty.handler.codec.http.websocketx.WebSocketFrame;
      22. import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
      23. import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
      24. import io.netty.util.CharsetUtil;

      25. public class MyWebSocketServerHandler extends
      26. SimpleChannelInboundHandler<Object> {

      27. private static final Logger logger = Logger
      28. .getLogger(WebSocketServerHandshaker.class.getName());

      29. private WebSocketServerHandshaker handshaker;

      30. @Override
      31. public void channelActive(ChannelHandlerContext ctx) throws Exception {

      32. // 添加
      33. Global.group.add(ctx.channel());

      34. System.out.println("客戶端與服務(wù)端連接開啟");

      35. }

      36. @Override
      37. public void channelInactive(ChannelHandlerContext ctx) throws Exception {

      38. // 移除
      39. Global.group.remove(ctx.channel());

      40. System.out.println("客戶端與服務(wù)端連接關(guān)閉");

      41. }

      42. @Override
      43. protected void messageReceived(ChannelHandlerContext ctx, Object msg)
      44. throws Exception {

      45. if (msg instanceof FullHttpRequest) {

      46. handleHttpRequest(ctx, ((FullHttpRequest) msg));

      47. } else if (msg instanceof WebSocketFrame) {

      48. handlerWebSocketFrame(ctx, (WebSocketFrame) msg);

      49. }

      50. }

      51. @Override
      52. public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
      53. ctx.flush();
      54. }

      55. private void handlerWebSocketFrame(ChannelHandlerContext ctx,
      56. WebSocketFrame frame) {

      57. // 判斷是否關(guān)閉鏈路的指令
      58. if (frame instanceof CloseWebSocketFrame) {
      59. handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame
      60. .retain());
      61. }

      62. // 判斷是否ping消息
      63. if (frame instanceof PingWebSocketFrame) {
      64. ctx.channel().write(
      65. new PongWebSocketFrame(frame.content().retain()));
      66. return;
      67. }

      68. // 本例程僅支持文本消息,不支持二進(jìn)制消息
      69. if (!(frame instanceof TextWebSocketFrame)) {

      70. System.out.println("本例程僅支持文本消息,不支持二進(jìn)制消息");

      71. throw new UnsupportedOperationException(String.format(
      72. "%s frame types not supported", frame.getClass().getName()));
      73. }

      74. // 返回應(yīng)答消息
      75. String request = ((TextWebSocketFrame) frame).text();

      76. System.out.println("服務(wù)端收到:" + request);

      77. if (logger.isLoggable(Level.FINE)) {
      78. logger
      79. .fine(String.format("%s received %s", ctx.channel(),
      80. request));
      81. }

      82. TextWebSocketFrame tws = new TextWebSocketFrame(new Date().toString()
      83. + ctx.channel().id() + ":" + request);

      84. // 群發(fā)
      85. Global.group.writeAndFlush(tws);

      86. // 返回【誰(shuí)發(fā)的發(fā)給誰(shuí)】
      87. // ctx.channel().writeAndFlush(tws);

      88. }

      89. private void handleHttpRequest(ChannelHandlerContext ctx,
      90. FullHttpRequest req) {

      91. if (!req.getDecoderResult().isSuccess()
      92. || (!"websocket".equals(req.headers().get("Upgrade")))) {

      93. sendHttpResponse(ctx, req, new DefaultFullHttpResponse(
      94. HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));

      95. return;
      96. }

      97. WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
      98. "ws://localhost:7397/websocket", null, false);

      99. handshaker = wsFactory.newHandshaker(req);

      100. if (handshaker == null) {
      101. WebSocketServerHandshakerFactory
      102. .sendUnsupportedWebSocketVersionResponse(ctx.channel());
      103. } else {
      104. handshaker.handshake(ctx.channel(), req);
      105. }

      106. }

      107. private static void sendHttpResponse(ChannelHandlerContext ctx,
      108. FullHttpRequest req, DefaultFullHttpResponse res) {

      109. // 返回應(yīng)答給客戶端
      110. if (res.getStatus().code() != 200) {
      111. ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(),
      112. CharsetUtil.UTF_8);
      113. res.content().writeBytes(buf);
      114. buf.release();
      115. }

      116. // 如果是非Keep-Alive,關(guān)閉連接
      117. ChannelFuture f = ctx.channel().writeAndFlush(res);
      118. if (!isKeepAlive(req) || res.getStatus().code() != 200) {
      119. f.addListener(ChannelFutureListener.CLOSE);
      120. }
      121. }

      122. private static boolean isKeepAlive(FullHttpRequest req) {

      123. return false;
      124. }

      125. @Override
      126. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
      127. throws Exception {

      128. cause.printStackTrace();
      129. ctx.close();

      130. }

      131. }



      NettyServer.java


      1. package com.itstack.netty.websocket;

      2. import io.netty.bootstrap.ServerBootstrap;
      3. import io.netty.channel.Channel;
      4. import io.netty.channel.ChannelOption;
      5. import io.netty.channel.EventLoopGroup;
      6. import io.netty.channel.nio.NioEventLoopGroup;
      7. import io.netty.channel.socket.nio.NioServerSocketChannel;

      8. public class NettyServer {

      9. public static void main(String[] args) {
      10. new NettyServer().run();
      11. }
      12. public void run(){
      13. EventLoopGroup bossGroup = new NioEventLoopGroup();
      14. EventLoopGroup workGroup = new NioEventLoopGroup();
      15. try {
      16. ServerBootstrap b = new ServerBootstrap();
      17. b.group(bossGroup, workGroup);
      18. b.channel(NioServerSocketChannel.class);
      19. b.childHandler(new ChildChannelHandler());
      20. System.out.println("服務(wù)端開啟等待客戶端連接 ... ...");
      21. Channel ch = b.bind(7397).sync().channel();
      22. ch.closeFuture().sync();
      23. } catch (Exception e) {
      24. e.printStackTrace();
      25. }finally{
      26. bossGroup.shutdownGracefully();
      27. workGroup.shutdownGracefully();
      28. }
      29. }
      30. }



      客戶端:




      1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
      2. <html xmlns="http://www./1999/xhtml">
      3. <head>
      4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      5. <title>無(wú)標(biāo)題文檔</title>
      6. </head>
      7. </head>
      8. <script type="text/javascript">
      9. var socket;
      10. if(!window.WebSocket){
      11.  
      12. window.WebSocket = window.MozWebSocket;
      13. }
      14.  
      15. if(window.WebSocket){
      16. socket = new WebSocket("ws://localhost:7397/websocket");
      17. socket.onmessage = function(event){
      18.  
      19. var ta = document.getElementById('responseText');
      20. ta.value += event.data+"\r\n";
      21. };
      22.  
      23. socket.onopen = function(event){
      24.  
      25. var ta = document.getElementById('responseText');
      26. ta.value = "打開WebSoket 服務(wù)正常,瀏覽器支持WebSoket!"+"\r\n";
      27. };
      28.  
      29. socket.onclose = function(event){
      30.  
      31. var ta = document.getElementById('responseText');
      32. ta.value = "";
      33. ta.value = "WebSocket 關(guān)閉"+"\r\n";
      34. };
      35. }else{
      36. alert("您的瀏覽器不支持WebSocket協(xié)議!");
      37. }
      38.  
      39. function send(message){
      40. if(!window.WebSocket){return;}
      41. if(socket.readyState == WebSocket.OPEN){
      42. socket.send(message);
      43. }else{
      44. alert("WebSocket 連接沒(méi)有建立成功!");
      45. }
      46. }
      47. </script>
      48. <body>
      49. <form onSubmit="return false;">
      50. <input type = "text" name="message" value="Netty The Sinper"/>
      51. <br/><br/>
      52. <input type="button" value="發(fā)送 WebSocket 請(qǐng)求消息" onClick="send(this.form.message.value)"/>
      53. <hr color="blue"/>
      54. <h3>服務(wù)端返回的應(yīng)答消息</h3>
      55. <textarea id="responseText" style="width: 1024px;height: 300px;"></textarea>
      56. </form>
      57. </body>
      58. </html>



      測(cè)試運(yùn)行:

      1、啟動(dòng)NettyServer

      2、啟動(dòng)支持websocket的瀏覽器,打開index.html【開啟2個(gè)以上】

      3、服務(wù)端輸出結(jié)果:

      服務(wù)端開啟等待客戶端連接 ... ...
      客戶端與服務(wù)端連接開啟
      客戶端與服務(wù)端連接開啟
      客戶端與服務(wù)端連接關(guān)閉
      客戶端與服務(wù)端連接開啟
      服務(wù)端收到:Netty The Sinper
      服務(wù)端收到:Netty The Sinper
      服務(wù)端收到:www.
      服務(wù)端收到:www.
      服務(wù)端收到:您好,您也在學(xué)習(xí)Netty嗎?
      服務(wù)端收到:是的!一起學(xué)習(xí)吧!
      服務(wù)端收到:嗯,到群里來(lái)吧5360392
      服務(wù)端收到:好的,么么大

      4、客戶端輸出截圖:

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多