0

我是使用 cometd 的新手,我正在尝试使用它在我的 Web 应用程序中启用推送消息。在使用 jetty 8 成功测试后,我尝试将 cometd 直接插入我的 Web 应用程序,因此我以这种方式更改了我的 web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     id="erp-hm" version="3.0">
<display-name>ERP HM</display-name>
<welcome-file-list>
    <welcome-file>inicio.xhtml</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet>
    <servlet-name>Logout Servlet</servlet-name>
    <servlet-class>com.hrgi.web.seguranca.ControladorLogout</servlet-class>
    <load-on-startup>3</load-on-startup>
</servlet>
<servlet>
    <servlet-name>push</servlet-name>
    <servlet-class>org.cometd.server.CometdServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>
<servlet>
    <servlet-name>Download Servlet</servlet-name>
    <servlet-class>com.hrgi.web.erp.DownloadArquivoServlet</servlet-class>
    <load-on-startup>4</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Logout Servlet</servlet-name>
    <url-pattern>/j_spring_security_logout</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>push</servlet-name>
    <url-pattern>/push/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Download Servlet</servlet-name>
    <url-pattern>*.pdf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>com.sun.faces.expressionFactory</param-name>
    <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
    <param-name>com.sun.faces.enableMissingResourceLibraryDetection</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/appContext.xml</param-value>
</context-param>
<context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>
<context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Production</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
</context-param>
<context-param>
    <param-name>org.apache.myfaces.ERROR_HANDLING</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
    <param-value>/inicio.xhtml</param-value>
</context-param>
<listener>
    <listener-class>com.hrgi.web.erp.DataServletContextListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <async-supported>true</async-supported>
</filter>
<filter>
    <filter-name>filtroLimpadorCache</filter-name>
    <filter-class>com.hrgi.web.seguranca.FiltroLimpadorCache</filter-class>
    <async-supported>true</async-supported>
</filter>
<filter-mapping>
    <filter-name>filtroLimpadorCache</filter-name>
    <url-pattern>/login*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>
<error-page>
    <error-code>403</error-code>
    <location>/acesso_negado.xhtml</location>
</error-page>
<error-page>
    <error-code>404</error-code>
    <location>/endereco_invalido.xhtml</location>
</error-page>
<session-config>
    <session-timeout>600</session-timeout>
</session-config>
</web-app> 

javascript 客户端第一次尝试连接时出现 400 错误:

jQuery(document).ready(function ($) {
cometd = $.cometd;
cometd.configure('https://localhost:8181/erp-web/push/');   
cometd.handshake();
});

当 java 客户端尝试在频道中发布时,我得到了这个:

[#|2012-12-05T19:54:35.543+0000|INFO|oracle-glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=122;_ThreadName=Thread-8;|05/12/2012 - 19:54:35 [HttpClient-122] INFO org.cometd.client.BayeuxClient.2002219263 - Messages failed [{id=12, supportedConnectionTypes=[long-polling], channel=/meta/handshake, version=1.0}]
java.net.ProtocolException: Unexpected response 302: TransportExchange@5f331f10=POST//localhost:8181/erp-web/push/handshake#CONTENT(1ms)->COMPLETED(0ms)sent=22ms
at org.cometd.client.BayeuxClient$PublishTransportListener.onProtocolError(BayeuxClient.java:1161)
at org.cometd.client.transport.LongPollingTransport$TransportExchange.onResponseComplete(LongPollingTransport.java:324)
at org.eclipse.jetty.client.HttpExchange$Listener.onResponseComplete(HttpExchange.java:1158)
at org.eclipse.jetty.client.HttpExchange.setStatus(HttpExchange.java:305)
at org.eclipse.jetty.client.AbstractHttpConnection$Handler.messageComplete(AbstractHttpConnection.java:337)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:861)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
at org.eclipse.jetty.client.AsyncHttpConnection.handle(AsyncHttpConnection.java:133)
at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:196)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:722)
|#]

有什么我可以解决的吗?或者我应该使用jetty来实现cometd服务器??

4

1 回答 1

1

对于 JS 客户端问题,我怀疑您应该在客户端禁用 WebSocket,因为 Glassfish 不支持 WebSocket,而 CometD 仅适用于 Jetty 的 WebSocket 实现。请参阅此处了解如何配置 JS 客户端 WebSocket 传输。

对于 Java 客户端问题,如果您得到 302 可能是因为您用于配置的BayeuxClientURL 不是 CometD URL。CometD servlet 从不回复 302,因此您一定打错了 URL。

于 2012-12-05T22:39:42.927 回答