已建立大气插座但未接收/发送数据

时间:2012-10-30 19:26:55

标签: java websocket atmosphere

我正在关注Atmosphere tutorial以获得基本的聊天应用程序设置。目前,当运行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);
    });

});

1 个答案:

答案 0 :(得分:1)

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

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

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

确保使用命令提示符运行该命令。我之前通过admin gui启用了Web套接字,并且似乎没有正确应用它。运行上面的命令并尝试上面的代码后,它工作正常。