我做了一件球衣气氛服务 当我尝试订阅该主题时,它已连接但未接收消息
我正在尝试从java客户端向javascript客户端发送消息
服务器代码
@Path("/pubsub/{topic}")
public class Eventer {
private
@PathParam("topic")
Broadcaster topic;
@GET
public SuspendResponse<String> subscribe() {
return new SuspendResponse.SuspendResponseBuilder<String>()
.broadcaster(topic)
.outputComments(true)
.addListener(new EventsLogger())
.build();
}
@POST
@Broadcast
@Produces("text/html;charset=ISO-8859-1")
public Broadcastable publish(@FormParam("message") String message) {
return new Broadcastable(message, "", topic);
}
}
JavaScript客户端
var myName = false;
var author = null;
var logged = false;
var socket = $.atmosphere;
var subSocket;
var transport = 'websocket';
var fallbackTransport = 'long-polling'
var connected = false;
var uuid = 0;
function connect() {
// We are now ready to cut the request
var request = { url: 'http://localhost:8888/factory/factory/pubsub/Light_FF_Bath_Ceiling' ,
transport: transport,
fallbackTransport: fallbackTransport};
request.onOpen = function (response) {
alert('onOpen');
};
request.onReopen = function (response) {
alert('onReopen');
};
request.onMessage = function (response) {
alert(response);
};
request.onClose = function (response) {
subSocket.push(JSON.stringify({ author: author, message: 'disconnecting' }));
};
request.onError = function (response) {
alert('onError');
};
request.onReconnect = function (request, response) {
};
subSocket = socket.subscribe(request);
}
connect();
Web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e194 -->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<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>
<load-on-startup>1</load-on-startup>
<!-- 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>/factory/*</url-pattern>
</servlet-mapping>
</web-app>