Netty Websocket服务器端和javascript客户端编程

[Java] Netty Websocket Server Javascript Clie


WebSocket协议的出现无疑是 HTML5 中最令人兴奋的功能特性之一,它能够很好地替代Comet技术以及Flash的XmlSocket来实现基于HTTP协议的双向通信。目前主流的浏览器,如Chrome、Firefox、IE10、Opera10、Safari等都已经支持WebSocket。另外,在服务端也出现了一些不错的WebSocket项目,如Resin、Jetty7、pywebsocket等。不过,本文将介绍的是如何使用强大的Netty框架(Netty-3.5.7.Final)来实现WebSocket服务端。

Netty3框架的性能优势已无需多说,但更让开发者舒心的是,Netty3还为大家提供了非常丰富的协议实现,包括HTTP、Protobuf、WebSocket等,开发者们可以很轻松的实现自己的Socket Server。按照Netty3的常规思路,我们需要准备以下3个文件:

1、WebSocketServer.java
2、WebSocketServerHandler.java
3、WebSocketServerPipelineFactory.java


以上3个文件分别包含了主程序的逻辑、服务的处理逻辑以及Socket Pipeline的设置逻辑。Java代码实现如下:

WebSocketServer.java
[java] view plain copy print ?
  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Executors;
  3. import org.jboss.netty.bootstrap.ServerBootstrap;
  4. import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
  5. public class WebSocketServer
  6. {
  7. private final int port;
  8. public WebSocketServer(int port) {
  9. this.port = port;
  10. }
  11. public void run() {
  12. // 设置 Socket channel factory
  13. ServerBootstrap bootstrap = new ServerBootstrap(
  14. new NioServerSocketChannelFactory(
  15. Executors.newCachedThreadPool(),
  16. Executors.newCachedThreadPool()));
  17. // 设置 Socket pipeline factory
  18. bootstrap.setPipelineFactory(new WebSocketServerPipelineFactory());
  19. // 启动服务,开始监听
  20. bootstrap.bind(new InetSocketAddress(port));
  21. // 打印提示信息
  22. System.out.println("Web socket server started at port " + port + '.');
  23. System.out.println("Open your browser and navigate to http://localhost:" + port + '/');
  24. }
  25. public static void main(String[] args) {
  26. int port;
  27. if (args.length > 0) {
  28. port = Integer.parseInt(args[0]);
  29. } else {
  30. port = 8080;
  31. }
  32. new WebSocketServer(port).run();
  33. }
  34. }
WebSocketServerPipelineFactory.java
[java] view plain copy print ?
  1. import static org.jboss.netty.channel.Channels.*;
  2. import org.jboss.netty.channel.ChannelPipeline;
  3. import org.jboss.netty.channel.ChannelPipelineFactory;
  4. import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
  5. import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
  6. import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
  7. public class WebSocketServerPipelineFactory implements ChannelPipelineFactory {
  8. public ChannelPipeline getPipeline() throws Exception {
  9. // pipeline 的配置与 逻辑
  10. ChannelPipeline pipeline = pipeline();
  11. pipeline.addLast("decoder", new HttpRequestDecoder());
  12. pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
  13. pipeline.addLast("encoder", new HttpResponseEncoder());
  14. pipeline.addLast("handler", new WebSocketServerHandler());
  15. return pipeline;
  16. }
  17. }
WebSocketServerHandler.java
[java] view plain copy print ?
  1. import static org.jboss.netty.handler.codec.http.HttpHeaders.*;
  2. import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.*;
  3. import static org.jboss.netty.handler.codec.http.HttpMethod.*;
  4. import static org.jboss.netty.handler.codec.http.HttpResponseStatus.*;
  5. import static org.jboss.netty.handler.codec.http.HttpVersion.*;
  6. import org.jboss.netty.buffer.ChannelBuffer;
  7. import org.jboss.netty.buffer.ChannelBuffers;
  8. import org.jboss.netty.channel.ChannelFuture;
  9. import org.jboss.netty.channel.ChannelFutureListener;
  10. import org.jboss.netty.channel.ChannelHandlerContext;
  11. import org.jboss.netty.channel.ExceptionEvent;
  12. import org.jboss.netty.channel.MessageEvent;
  13. import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
  14. import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
  15. import org.jboss.netty.handler.codec.http.HttpHeaders;
  16. import org.jboss.netty.handler.codec.http.HttpRequest;
  17. import org.jboss.netty.handler.codec.http.HttpResponse;
  18. import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
  19. import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
  20. import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
  21. import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
  22. import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
  23. import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
  24. import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
  25. import org.jboss.netty.logging.InternalLogger;
  26. import org.jboss.netty.logging.InternalLoggerFactory;
  27. import org.jboss.netty.util.CharsetUtil;
  28. public class WebSocketServerHandler extends SimpleChannelUpstreamHandler
  29. {
  30. private static final InternalLogger logger = InternalLoggerFactory
  31. .getInstance(WebSocketServerHandler.class);
  32. private static final String WEBSOCKET_PATH = "/websocket";
  33. private WebSocketServerHandshaker handshaker;
  34. @Override
  35. public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
  36. throws Exception {
  37. // 处理接受消息
  38. Object msg = e.getMessage();
  39. if (msg instanceof HttpRequest) {
  40. handleHttpRequest(ctx, (HttpRequest) msg);
  41. } else if (msg instanceof WebSocketFrame) {
  42. handleWebSocketFrame(ctx, (WebSocketFrame) msg);
  43. }
  44. }
  45. @Override
  46. public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
  47. throws Exception {
  48. // 处理异常情况
  49. e.getCause().printStackTrace();
  50. e.getChannel().close();
  51. }
  52. private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req)
  53. throws Exception {
  54. // 只接受 HTTP GET 请求
  55. if (req.getMethod() != GET) {
  56. sendHttpResponse(ctx, req, new DefaultHttpResponse(HTTP_1_1,
  57. FORBIDDEN));
  58. return;
  59. }
  60. // Websocket 握手开始
  61. WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
  62. getWebSocketLocation(req), null, false);
  63. handshaker = wsFactory.newHandshaker(req);
  64. if (handshaker == null) {
  65. wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel());
  66. } else {
  67. handshaker.handshake(ctx.getChannel(), req).addListener(
  68. WebSocketServerHandshaker.HANDSHAKE_LISTENER);
  69. }
  70. }
  71. private void handleWebSocketFrame(ChannelHandlerContext ctx,
  72. WebSocketFrame frame) {
  73. // Websocket 握手结束
  74. if (frame instanceof CloseWebSocketFrame) {
  75. handshaker.close(ctx.getChannel(), (CloseWebSocketFrame) frame);
  76. return;
  77. } else if (frame instanceof PingWebSocketFrame) {
  78. ctx.getChannel().write(new PongWebSocketFrame(frame.getBinaryData()));
  79. return;
  80. } else if (!(frame instanceof TextWebSocketFrame)) {
  81. throw new UnsupportedOperationException(String.format("%s frame types not supported",
  82. frame.getClass().getName()));
  83. }
  84. // 处理接受到的数据(转成大写)并返回
  85. String request = ((TextWebSocketFrame) frame).getText();
  86. if (logger.isDebugEnabled()) {
  87. logger.debug(String.format("Channel %s received %s", ctx.getChannel().getId(), request));
  88. }
  89. ctx.getChannel().write(new TextWebSocketFrame(request.toUpperCase()));
  90. }
  91. private static void sendHttpResponse(ChannelHandlerContext ctx,
  92. HttpRequest req, HttpResponse res) {
  93. // 返回 HTTP 错误页面
  94. if (res.getStatus().getCode() != 200) {
  95. res.setContent(ChannelBuffers.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8));
  96. setContentLength(res, res.getContent().readableBytes());
  97. }
  98. // 发送返回信息并关闭连接
  99. ChannelFuture f = ctx.getChannel().write(res);
  100. if (!isKeepAlive(req) || res.getStatus().getCode() != 200) {
  101. f.addListener(ChannelFutureListener.CLOSE);
  102. }
  103. }
  104. private static String getWebSocketLocation(HttpRequest req) {
  105. return "ws://" + req.getHeader(HttpHeaders.Names.HOST) + WEBSOCKET_PATH;
  106. }
  107. }

以上代码的逻辑还是比较清晰的:首先,在WebSocketServer中设置WebSocketServerPipelineFactory;然后,在WebSocketServerPipelineFactory中设置WebSocketServerHandler;接着,在WebSocketServerHandler处理请求并返回结果;其中,最重要的处理逻辑位于handleWebSocketFrame方法中,也就是把获取到的请求信息全部转化成大写并返回。最后,运行WebSocketServer.java就可以启动WebSocket服务,监听本地的8080端口。至此,WebSocket服务端已经全部准备就绪。这里,其实我们已经同时开发了一个简单的HTTP服务器,界面截图如下:



接下来,我们需要准备使用Javascript实现的WebSocket客户端,实现非常简单,Javascript代码实现如下:

websocket.html
[html] view plain copy print ?
  1. <html><head><title>Web Socket Client</title></head>
  2. <body>
  3. <script type="text/javascript">
  4. var socket;
  5. if (!window.WebSocket) {
  6. window.WebSocket = window.MozWebSocket;
  7. }
  8. // Javascript Websocket Client
  9. if (window.WebSocket) {
  10. socket = new WebSocket("ws://localhost:8080/websocket");
  11. socket.onmessage = function(event) {
  12. var ta = document.getElementById('responseText');
  13. ta.value = ta.value + '\n' + event.data
  14. };
  15. socket.onopen = function(event) {
  16. var ta = document.getElementById('responseText');
  17. ta.value = "Web Socket opened!";
  18. };
  19. socket.onclose = function(event) {
  20. var ta = document.getElementById('responseText');
  21. ta.value = ta.value + "Web Socket closed";
  22. };
  23. } else {
  24. alert("Your browser does not support Web Socket.");
  25. }
  26. // Send Websocket data
  27. function send(message) {
  28. if (!window.WebSocket) { return; }
  29. if (socket.readyState == WebSocket.OPEN) {
  30. socket.send(message);
  31. } else {
  32. alert("The socket is not open.");
  33. }
  34. }
  35. </script>
  36. <h3>Send :</h3>
  37. <form onsubmit="return false;">
  38. <input type="text" name="message" value="Hello World!"/><input type="button" value="Send Web Socket Data" onclick="send(this.form.message.value)" />
  39. <h3>Receive :</h3>
  40. <textarea id="responseText" style="width:500px;height:300px;"></textarea>
  41. </form>
  42. </body>
  43. </html>

以上Javascript代码的逻辑很好理解:即创建一个指向对应WebSocket地址(ws://localhost:8080/websocket)的Socket连接,进而进行发送和获取操作。其实,我们只需要把websocket.html文件放置到任意的HTTP服务器上,并打开对应URL地址,就可以看到以下的Demo界面:

Netty Websocket服务器端和javascript客户端编程_第1张图片

输入文字“Hello World”并点击“Send Web Socket Data”按钮就可以向WebSocket的服务端发送消息了。从上图中我们还可以看到,在“Receive”下方的输出框中看到返回的消息(大写过的HELLO WORLD文字),这样一次基本的信息交互就完成了。当然,此时如果把服务端关闭,输出框中则会看到“Web Socket closed”信息。

来源:http://blog.csdn.net/shagoo/article/details/8028813

你可能感兴趣的:(Netty Websocket服务器端和javascript客户端编程)