1

我正在按照 Atmosphere教程获取基本的聊天应用程序设置。目前在运行 javascript 时,它似乎能够使用 Web 套接字建立与服务器的连接。我在 Chrome 控制台中得到以下信息:

Invoking executeWebSocket core.js:2298
Using URL: ws://localhost:8080/web-transport/chat?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=1.1&X-Atmosphere-Transport=websocket&X-Cache-Date=0&Content-Type=application/json core.js:2298
Websocket successfully opened 

但是,当我尝试发布数据 (subSocket.push(...)) 时,似乎什么也没发生。我已经尝试调试 AtmosphereHandlerService 但它似乎从未被击中(并且没有生成日志。

我错过了什么会导致这种情况?

到目前为止,我有以下内容:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:j2ee="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    http://java.sun.com/xml/ns/j2ee/web-app_2.5.xsd">
    <description>AtmosphereServlet</description>
    <display-name>AtmosphereServlet</display-name>
    <servlet>
        <description>AtmosphereServlet</description>
        <servlet-name>AtmosphereServlet</servlet-name>
        <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
        <!-- If you want to use Servlet 3.0 -->
        <!-- <async-supported>true</async-supported> -->
        <!-- List of init-param --> 
    </servlet>
    <servlet-mapping>
        <servlet-name>AtmosphereServlet</servlet-name>
        <!-- Any mapping -->
        <url-pattern>/chat/*</url-pattern>
    </servlet-mapping>
</web-app>

ChatAtmosphereHandler.java

@AtmosphereHandlerService(path = "/chat")
public class ChatAtmosphereHandler implements AtmosphereHandler {

    public void onRequest(AtmosphereResource resource) throws IOException {

        AtmosphereRequest request = resource.getRequest();

        if (request.getMethod().equalsIgnoreCase("GET")) {
            resource.suspend();
        } else if (request.getMethod().equalsIgnoreCase("POST")) {
            resource.getBroadcaster().broadcast(request.getReader().readLine().trim());
        }

    }

    public void onStateChange(AtmosphereResourceEvent event) throws IOException {

        AtmosphereResource resource = event.getResource();
        AtmosphereResponse response = resource.getResponse();

        if (resource.isSuspended()) {

            response.getWriter().write("Hello");

            switch (resource.transport()) {
                case JSONP:
                case LONG_POLLING:
                    event.getResource().resume();
                    break;
                case WEBSOCKET:
                case STREAMING:
                    response.getWriter().flush();
                    break;
            }

        } else if (!event.isResuming()) {
            event.broadcaster().broadcast("bye bye");
        }

    }

    public void destroy() { 
    }

}

Maven依赖:

 <dependency>
        <groupId>org.atmosphere</groupId>
        <artifactId>atmosphere-runtime</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.atmosphere</groupId>
        <artifactId>atmosphere-jersey</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>eu.infomas</groupId>
        <artifactId>annotation-detector</artifactId>
        <version>3.0.1</version>
    </dependency>

最后是javascript:

$(document).ready(function() {

    var socket = $.atmosphere;
    var subSocket = null;

    var request = { 
        url: 'http://localhost:8080/web-transport/' + 'chat',
        contentType : "application/json",
        logLevel : 'debug',
        transport : 'websocket' ,
        fallbackTransport: 'long-polling'
    };

    request.onOpen = function(response) {
        console.log('onopen', response);
    };

    request.onReconnect = function (request, response) {
        console.log('onreconnect', request, response);
    };

    request.onMessage = function (response) {
        console.log('onmessage', response);
    };

    request.onError = function(response) {
        console.log('onerror', response);
    };

    $('#send').click(function(){
        subSocket.push(JSON.stringify({ author: 'me', message: 'hello' }));
    });

    $('#subscribe').click(function(){
        subSocket = socket.subscribe(request);
    });

});
4

1 回答 1

1

我终于发现问题不在于代码或氛围,而在于 Glassfish。

解决方案是使用以下命令启用 Web 套接字:

asadmin set configs.config.server-config.network-config.protocols.protocol.http-listener-1.http.websockets-support-enabled=true

确保使用命令提示符运行命令。我之前通过管理 gui 启用了网络套接字,但不知何故,它似​​乎没有正确应用。在我运行了上面的命令并尝试了上面的代码之后,它运行良好。

于 2012-11-07T21:58:41.650 回答