简体   繁体   English

建立了大气套接字,但是没有接收/发送数据

[英]Atmosphere socket established but no data being received/sent

I am following the Atmosphere tutorial to get a basic chat application setup. 我正在关注Atmosphere 教程,以获取基本的聊天应用程序设置。 Currently when running the javascript, it appears to be able to establish a connection to the server using web sockets. 当前,在运行javascript时,似乎可以使用Web套接字建立与服务器的连接。 I get the following in the Chrome console: 我在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 

However, when I attempt to publish data (subSocket.push(...)) nothing seems to happen. 但是,当我尝试发布数据(subSocket.push(...))时,似乎什么都没有发生。 I have tried debugging the AtmosphereHandlerService but it never seems to be hit (and no logs are being generated. 我已经尝试调试AtmosphereHandlerService,但似乎从未被击中(并且未生成任何日志。

What am I missing that would cause this? 我错过了什么会导致这种情况?

So far I have the following: 到目前为止,我有以下内容:

web.xml 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 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: 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>

and finally the javascript: 最后是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);
    });

});

I finally figured out that the problem wasn't with the code or atmosphere, but with Glassfish. 我终于发现问题不在于代码或环境,而在于Glassfish。

The solution is to enable web sockets using the following command: 解决方案是使用以下命令启用Web套接字:

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. 我以前通过管理员gui启用了Web套接字,但似乎未正确应用它。 After I ran the above command and tried the above code it worked fine. 在运行上述命令并尝试了上面的代码后,它运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM