Board logo

标题: 反向 Ajax,第 3 部分 Web 服务器和 Socket.IO(1) [打印本页]

作者: look_w    时间: 2018-10-17 18:21     标题: 反向 Ajax,第 3 部分 Web 服务器和 Socket.IO(1)

简介现今,用户都期待能够通过 Web 访问快速的动态访问应用。本  向您展示如何使用 Reverse Ajax 技术来开发事件驱动的 Web 应用程序。 介绍了 Reverse Ajax、轮询、流、Comet 和长轮询。在您了解了如何通过 HTTP 使用 Comet 之后,就会发现长轮询是可靠地实现 Reverse Ajax 的最佳方式,因为目前所有浏览器都提供了这方面的支持。 展示了如何使用 WebSocket 实现 Reverse Ajax。代码示例有助于说明 WebSocket、FlashSocket、服务器端的约束、请求范围内的服务和暂停历时长久的请求。
本文将深入研究如何在不同 Web 容器和 API 的 Web 应用程序(Servlet 3.0 和 Jetty Continuation)中使用 Comet 和 WebSocket。了解如何通过抽象库(如 Socket.IO)透明地使用 Comet 和 WebSocket。Socket.IO 使用功能检测来决定是否将通过 WebSocket、AJAX 长轮询或 Flash 等来创建连接。
您可以 。
先决条件在理想情况下,要最大限度地充分利用本文,则应该了解 JavaScript 和 Java。本文创建的示例是使用 Google Guice 构建的。Google Guice 是用 Java 编写的依赖项注入框架。要理解本文内容,则需要熟悉依赖项注入框架的概念,比如 Guice、Spring 或 Pico。
要运行本文中的示例,还需要使用最新版的 Maven 和 JDK (请参阅 )。
Comet 和                WebSocket 的服务器解决方案您在  中应该已经了解到,Comet(长轮询或流)要求服务器在潜在的长延迟后能够暂停、重启或完成一个请求。 描述了服务器需要如何使用非阻塞 I/O 特性来处理一些连接,以及它们如何仅使用线程为请求提供服务(每请求线程模型)。您应该还了解到,WebSocket 的使用取决于服务器,并非所有的服务器都支持 WebSocket。
这一节将向您展示如何在 Jetty、Tomcat 和 Grizzly Web 服务器上使用 Comet 和 WebSocket(如果可以)。本文所提供的  包含了一个在 Jetty 和 Tomcat 上运行的简单聊天 Web 应用程序样例。本章节还将讨论支持 API 的下列应用服务器:Jboss、Glassfish 和 WebSphere。
JettyJetty 是一个 Web 服务器,它支持 Java Servlet 规范 3.0、WebSocket 和其他的集成规范。Jetty 具有以下特征:
核心的 Jetty 项目是由 Eclipse Foundation 管理。
自从版本 6 开始,Jetty 包含了一个异步 API,称为 Jetty Continuation,它可以充许暂停某个请求,稍后再重新开始该请求。表 1 显示了 Jetty 主要版本所支持的规范和 API 的图表。
表 1. Jetty 版本和支持 支持Jetty 6Jetty 7Jetty 8非阻塞 I/OXXXServlet 2.5XXXServlet 3.0
XXJetty Continuation (Comet)XXXWebSocket
XX
要通过 Comet 实现 Reverse Ajax ,可以使用 Jetty 所提供的 Continuation API,如下所示 清单 1:
清单 1.  用于 Comet 的 Jetty Continuation API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Pausing a request from a servlet's method (get, post, ...):

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

Continuation continuation = ContinuationSupport.getContinuation(req);
// optionally set a timeout to avoid suspending requests for too long
continuation.setTimeout(0);
// suspend the request
continuation.suspend();
// then hold a reference for future usage from another thread
continuations.offer(continuation);

}

// Then, from another thread which wants to send an event to the client:

while (!continuations.isEmpty()) {
    Continuation continuation = continuations.poll();
    HttpServletResponse response =
        (HttpServletResponse) continuation.getServletResponse();
    // write to the response
    continuation.complete();
}




完整的 Web 应用程序可在本文所附的  中找到。Jetty Continuation 存放在 JAR 文件中。您必须将此 JAR 文件放在 Web 应用程序的 WEB-INF/lib 文件夹中,以便能够使用 Jetty 的 Comet 特性。Jetty Continuation 也可以在 Jetty 6、Jetty  7 和 Jetty 8 上使用。
从 Jetty 7 开始,您还可以使用 WebSocket 特性。将 Jetty 的 WebSocket JAR 文件放入 Web 应用程序的 WEB-INF/lib 文件夹中,以便获得访问 Jetty 的 WebSocket API 的访问权,如 清单 2 中所示:
清单 2. Jetty 的 WebSocket API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Implement the  doWebSocketConnect and returns an implementation of
//  WebSocket:

public final class ReverseAjaxServlet extends WebSocketServlet {
    @Override
    protected WebSocket doWebSocketConnect(HttpServletRequest request,
                                           String protocol) {
        return [...]
    }
}

// Sample implementation of  WebSocket:

class Endpoint implements WebSocket {
    Outbound outbound;
    public void onConnect(Outbound outbound) {
        this.outbound = outbound;   
    }
    public void onMessage(byte opcode, String data) {
        outbound.sendMessage("Echo: " + data);
        if("close".equals(data))
             outbound.disconnect();
    }
    public void onFragment(boolean more, byte opcode,
                           byte[] data, int offset, int length) {
    }
    public void onMessage(byte opcode, byte[] data,
                          int offset, int length) {
        onMessage(opcode, new String(data, offset, length));
    }
    public void onDisconnect() {
        outbound = null;
    }
}




可下载  中的 jetty-websocket 文件夹提供了一个聊天示例,它演示了如何使用 Jetty 提供的 WebSocket API。




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0