我正在尝试让Atmosphere的一个实例作为servlet运行,而不使用Web.xml和WebSocket函数。
映射似乎不起作用,因为我可以连接到服务器(localhost:8080),并且JavaScript告诉我WS连接正在起作用,但处理程序没有被识别(方法从未被调用)。WebSocketHandlerAdapter似乎从未被AtmosphereServlet使用过。
我发现的所有示例都使用Web.xml来设置servlet,但我需要能够以编程方式实例化我的servlet(Jetty容器中的Atmosphere和Jersey)。
我一直在使用这个示例Atmosphere Websocket Chat进行WebSocketHandlerAdapter设置,并使用Jersey Atmosphere-Servlet在Jetty中实例化我的Servlet。
以下是我实例化服务器/servlets:的代码
public Server create() throws Exception {
logger.info("creating http server");
Server server = new Server();
server.setStopAtShutdown(true);
// configure http
SelectChannelConnector httpConnector = new SelectChannelConnector();
httpConnector.setPort(config.getHttpPort());
server.setConnectors(new Connector[] { httpConnector });
handlers = new HandlerCollection();
handlers.setServer(server);
handlers.addHandler(createLoggingHandler());
handlers.addHandler(createFileHandler());
handlers.addHandler(createJerseyRestServletHandler());
server.setHandler(handlers);
return server;
}
private ServletContextHandler createJerseyRestServletHandler() {
ServletHolder atmosphereHolder = initAtmosphereServletHolder();
ServletHolder jerseyHolder = initJerseyServletHolder();
// atmosphere
atmosphereHolder.setInitParameter("org.atmosphere.useWebSocket", "true");
atmosphereHolder.setInitParameter("WebSocketProtocol", "ChatAtmosphereHandler");
atmosphereHolder.setAsyncSupported(true);
FilterHolder filterHolder = new FilterHolder(CrossOriginFilter.class);
filterHolder.setInitParameter("allowedOrigins", "*");
filterHolder.setInitParameter("allowedMethods", "GET, POST");
ServletContextHandler servletHandler = new ServletContextHandler(handlers, "/");
servletHandler.addServlet(atmosphereHolder, "/websocket/*");
servletHandler.addServlet(jerseyHolder, "/jersey/*");
servletHandler.addFilter(filterHolder, "/*", null);
return servletHandler;
}
private ServletHolder initJerseyServletHolder() {
...
}
private ServletHolder initAtmosphereServletHolder() {
AtmosphereServlet atmosServlet = new AtmosphereServlet();
ServletHolder atmosphere = new ServletHolder(atmosServlet);
return atmosphere;
}
处理程序websocket:
@WebSocketHandlerService(path = "/websocket", broadcaster = SimpleBroadcaster.class)
public class ChatAtmosphereHandler extends WebSocketHandlerAdapter {
private final ObjectMapper mapper = new ObjectMapper();
@Override
public void onOpen(WebSocket webSocket) throws IOException {
System.out.println("on Open");
webSocket.resource().setBroadcaster(BroadcasterFactory.getDefault().lookup("/websocket", true));
}
public void onTextMessage(WebSocket webSocket, String message) throws IOException {
System.out.println("on Message");
AtmosphereResource r = webSocket.resource();
Broadcaster b = r.getBroadcaster();
b.broadcast(mapper.writeValueAsString(mapper.readValue(message, Data.class)));
}
public final static class Data {
private String message;
private String author;
private long time;
public Data() {
this("", "");
}
public Data(String author, String message) {
this.author = author;
this.message = message;
this.time = new Date().getTime();
}
public String getMessage() {
return message;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public void setMessage(String message) {
this.message = message;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
}
}
最后是pom.xml
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>7.6.4.v20120524</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-websocket</artifactId>
<version>7.6.4.v20120524</version>
</dependency>
<dependency>
<groupId>eu.infomas</groupId>
<artifactId>annotation-detector</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-annotations</artifactId>
<version>1.1.0.beta3</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-jersey</artifactId>
<version>1.1.0.beta3</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-commons</artifactId>
<version>0.6.5</version>
</dependency>
我是Atmosphere的负责人。
你能粘贴服务器的日志吗?此外,用于连接的URL应该是localhost:8080/websocket/<something>
。
第二,你为什么要定义泽西Servlet?Atmosphere不需要这样,只需定义AtmosphereServlet即可。
如果可以的话,点击邮件列表了解更多信息。