Atmosphere socket established but no data being received/sent

有些话、适合烂在心里 提交于 2019-12-23 03:04:49

问题


I am following the Atmosphere tutorial to get a basic chat application setup. Currently when running the javascript, it appears to be able to establish a connection to the server using web sockets. I get the following in the Chrome console:

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 

However, when I attempt to publish data (subSocket.push(...)) nothing seems to happen. I have tried debugging the AtmosphereHandlerService but it never seems to be hit (and no logs are being generated.

What am I missing that would cause this?

So far I have the following:

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 dependencies:

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

and finally the 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:


I finally figured out that the problem wasn't with the code or atmosphere, but with Glassfish.

The solution is to enable web sockets using the following command:

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

Make sure that you run the command using the command prompt. I previously enabled web sockets via the admin gui and somehow it seems to not have been applied correctly. After I ran the above command and tried the above code it worked fine.



来源:https://stackoverflow.com/questions/13146274/atmosphere-socket-established-but-no-data-being-received-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!