1

我试图在不使用 Web.xml 和使用 WebSocket 功能的情况下让 Atmosphere 实例作为 servlet 运行。

似乎映射不起作用,因为我能够连接到服务器 (localhost:8080),并且 JavaScript 告诉我 WS 连接正在工作,但处理程序未被识别(从未调用方法)。AtmosphereServlet 似乎从未使用过 WebSocketHandlerAdapter。

我发现的所有示例都使用 Web.xml 来设置 servlet,但我需要能够以编程方式实例化我的 servlet(Jetty 容器中的 Atmosphere 和 Jersey)。

我一直在使用这个示例Atmosphere Websocket Chat来设置 WebSocketHandlerAdapter,并使用Jersey Atmosphere Servlet在 Jetty 中实例化我的 servlet。

这是我实例化我的服务器/servlet的代码:

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>
4

1 回答 1

1

我是气氛组长。

你可以粘贴你的服务器的日志吗?此外,URL 应该localhost:8080/websocket/<something>用于连接。

其次,你为什么要定义 Jersey Servlet?Atmosphere 不需要它,因此只需定义 AtmosphereServlet。

如果可以,请跳上邮件列表以获取更多信息。

于 2013-02-06T19:13:07.670 回答