我在使用cometD和Longpolling处理tomcat时遇到了问题。
我在firebug中看到所有请求都在ms区域,尽管民意调查应该保持至少1秒。服务器在ms时间内没有响应。
这是我在萤火虫中得到的:
8ms
dojo.j...ssed.js (Zeile 11610)
POST http://localhost:8080/chatty/cometd/connect
200 OK
4ms
dojo.j...ssed.js (Zeile 11610)
POST http://localhost:8080/chatty/cometd/connect
200 OK
2ms
dojo.j...ssed.js (Zeile 11610)
POST http://localhost:8080/chatty/cometd/connect
我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometdServlet</servlet-class>
<init-param>
<param-name>transports</param-name>
<param-value>org.cometd.websocket.server.JSONTransport</param-value>
</init-param>
<init-param>
<param-name>allowedTransports</param-name>
<param-value>org.cometd.server.transport.LongPollingTransport</param-value>
</init-param>
<init-param>
<param-name>services</param-name>
<param-value>com.xxxxx.StockPriceService</param-value>
</init-param>
<init-param>
<param-name>timeout</param-name>
<param-value>60000</param-value>
</init-param>
<init-param>
<param-name>logLevel</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>interval</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>long-polling.multiSessionInterval</param-name>
<param-value>2000</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>initializer</servlet-name>
<servlet-class>com.xxxxx.Initializer</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/cometd/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>continuation</filter-name>
<filter-class>org.eclipse.jetty.continuation.ContinuationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>continuation</filter-name>
<url-pattern>/cometd/*</url-pattern>
</filter-mapping>
我的pom: 安装 Maven的战争插件 org.cometd.javascript 的cometd-JavaScript的道场 org.mortbay.jetty 码头 - Maven的插件 10 /
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>bayeux-api</artifactId>
<version>${cometd-version}</version>
</dependency>
<dependency>
<groupId>org.cometd.javascript</groupId>
<artifactId>cometd-javascript-dojo</artifactId>
<version>${cometd-version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-server</artifactId>
<version>${cometd-version}</version>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-annotations</artifactId>
<version>${cometd-version}</version>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-client</artifactId>
<version>${cometd-version}</version>
</dependency>
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-websocket-jetty</artifactId>
<version>${cometd-version}</version>
<exclusions>
<exclusion>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jmx</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-websocket</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>${jetty-version}</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-version}</version>
</dependency>
</dependencies>
我确实想念longpoll配置吗?
答案 0 :(得分:2)
您的web.xml
确实是错误的。
首先,您无需指定transports
&lt; init-param&gt;如果您只想使用long-polling
传输。此参数适用于websocket
等其他传输。这解释为here。
参数allowedTransports
指定传输名称,而不是完全限定的类名。
这解释为here。
services
参数仅由AnnotationCometdServlet
读取,如here所述(参见第6.4.2.3.4节)。
答案 1 :(得分:0)
web.xml中的过滤器必须标记为true 例如org.eclipse.jetty.continuation.ContinuationFilter
答案 2 :(得分:0)
我不知道你是否解决了问题。我使用Faye Browser客户端(JS)遇到了同样的问题。在我的情况下,当我在项目中添加一个新的Transport类并且将它包含在我的web.xml中时开始工作。
见下文:
package org.foo.bar.http.transports;
import java.util.Enumeration;
import java.io.IOException;
import java.text.ParseException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.cometd.bayeux.server.ServerMessage;
import org.cometd.server.BayeuxServerImpl;
import org.cometd.server.ServerSessionImpl;
import org.cometd.server.transport.LongPollingTransport;
public class CrossOriginLongPollingTransport extends LongPollingTransport
{
public final static String PREFIX = "long-polling-cross-origin.json";
public final static String NAME = "cross-origin-long-polling";
public final static String MIME_TYPE_OPTION = "mimeType";
private boolean _jsonDebug = false;
private String _mimeType = "application/json;charset=UTF-8";
public CrossOriginLongPollingTransport(BayeuxServerImpl bayeux)
{
super(bayeux, NAME);
setOptionPrefix(PREFIX);
}
@Override
protected boolean isAlwaysFlushingAfterHandle()
{
return false;
}
@Override
protected void init()
{
super.init();
_jsonDebug = getOption(JSON_DEBUG_OPTION, _jsonDebug);
_mimeType = getOption(MIME_TYPE_OPTION, _mimeType);
}
@Override
public boolean accept(HttpServletRequest request)
{
return "POST".equals(request.getMethod());
}
@Override
protected ServerMessage.Mutable[] parseMessages(HttpServletRequest request) throws IOException, ParseException
{
String charset = request.getCharacterEncoding();
if (charset == null)
request.setCharacterEncoding("UTF-8");
String contentType = request.getContentType();
if (contentType == null || contentType.startsWith("application/json"))
return parseMessages(request.getReader(), _jsonDebug);
else if (contentType.startsWith("application/x-www-form-urlencoded")) {
return parseMessages(request.getParameterValues(MESSAGE_PARAM));
} else
throw new IOException("Invalid Content-Type " + contentType);
}
@Override
protected ServletOutputStream beginWrite(HttpServletRequest request, HttpServletResponse response, ServerSessionImpl session) throws IOException
{
response.setContentType(_mimeType);
ServletOutputStream output = response.getOutputStream();
output.write('[');
return output;
}
@Override
protected void endWrite(ServletOutputStream output, ServerSessionImpl session) throws IOException
{
output.write(']');
output.close();
}
}
之后,包含在你的web.xml中:
<init-param>
<param-name>transports</param-name>
<param-value>org.cometd.websocket.server.WebSocketTransport,org.foo.bar.http.transports.CrossOriginLongPollingTransport</param-value>
</init-param>
<init-param>
<param-name>allowedTransports</param-name>
<param-value>cross-origin-long-polling,long-polling,callback-polling,websocket</param-value>
</init-param>
它对我有用,我希望你也可以帮忙。
最佳。