看起来订阅没有到达我的服务器。
我的服务器端订阅方法在订阅时打印到Tomcat控制台。我加载网页时没有看到这个日志
Stack:Atmosphere 2.2.3,Atmosphere-Jersey 2.2.3,Jersey-Jason 1.6,Servlet 3,Java 7,Tomcat 7.0.56
的JavaScript
$(function () {
"use strict";
var content = $('#content');
var input = $('#input');
var status = $('#status');
var logged = false;
var socket = $.atmosphere;
var request = { url: 'http://localhost:8080/atmospherePOC/rest/subscription',
contentType : "application/json",
logLevel : 'debug',
transport : 'websocket' ,
trackMessageLength : true,
fallbackTransport: 'long-polling'};
request.onOpen = function(response) {
content.html($('<p>', { text: 'Atmosphere connected using ' + response.transport }));
};
request.onMessage = function (response) {
var message = response.responseBody;
try {
var json = jQuery.parseJSON(message);
} catch (e) {
console.log('This doesn\'t look like a valid JSON: ', message);
return;
}
content.append('<p><span style="color:">' + json.identifier + ' Buy' + json.buy +' Sell ' + json.sell +'</span>' + '</p>');
};
request.onClose = function(response) {
logged = false;
}
request.onError = function(response) {
content.html($('<p>', { text: 'Sorry, but there\'s some problem with your '
+ 'socket or the server is down' }));
};
socket.subscribe(request);
});
订阅处理程序:
@Path("subscription")
public class CurrencyPairSubscription {
@GET
@Suspend
public Broadcastable subscribeToQuote(@Context final BroadcasterFactory broadcasterFactory) {
System.out.println("New subscriber");
return new Broadcastable(broadcasterFactory.lookup("A_TOPIC", true));
}
}
Web.xml中
<?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>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
<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 -->
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.foo</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AtmosphereServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>BroadcasterServlet</servlet-name>
<servlet-class>com.foo.broadcaster.BroadcasterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BroadcasterServlet</servlet-name>
<url-pattern>/broadcasterServlet/*</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
经过大量的敲击我的头后,我得到了这个工作*。诀窍是不在查询字符串中传递标头。您可以在JavaScript中构建请求时完成此操作:
$(function () {
"use strict";
var content = $('#content');
var input = $('#input');
var status = $('#status');
var logged = false;
var socket = $.atmosphere;
var request = { url: 'http://localhost:8080/atmospherePOC/rest/subscription',
contentType : "application/json",
logLevel : 'debug',
transport : 'websocket' ,
trackMessageLength : true,
fallbackTransport: 'long-polling' ,
attachHeadersAsQueryString: false}
通过此更改,订阅请求将命中服务器。
*谨慎,整个应用程序无法正常工作,仍有问题,所以不能说100%这有效但从服务器端看到记录器是一个很好的指示