已建立大气插座但未接收/发送数据 [英] Atmosphere socket established but no data being received/sent

查看:178
本文介绍了已建立大气插座但未接收/发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注大气教程以获得基本的聊天应用程序设置。目前,当运行javascript时,它似乎能够使用Web套接字建立与服务器的连接。我在Chrome控制台中获得以下内容:

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 

然而,当我尝试发布数据(subSocket.push(...))似乎什么都没发生。我已经尝试调试AtmosphereHandlerService但它似乎永远不会被命中(并且没有生成日志。

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

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:

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);
    });

});


推荐答案

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

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

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

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

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

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.

这篇关于已建立大气插座但未接收/发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆