我使用了带有码头9嵌入式和平针织物的气氛。
我的服务器代码。
@Path( “/ {CHANNELNAME}”) @辛格尔顿 公共类ResourceChat {
@PathParam("channelName")
Broadcaster groupBroadcaster;
@Suspend
@GET
@Produces(MediaType.TEXT_PLAIN)
public Broadcastable suspend(@Context AtmosphereResource res, @Context BroadcasterFactory factory) {
Broadcaster privateBroadcaster = factory.lookup(res.session(), true);
privateBroadcaster.addAtmosphereResource(res);
return new Broadcastable(privateBroadcaster.getID(), groupBroadcaster);
}
@Broadcast(writeEntity = false)
@POST
@Produces(MediaType.TEXT_PLAIN)
public Broadcastable onMessage(String message) {
return new Broadcastable(message, groupBroadcaster);
}
}
我的客户代码。
var socket = atmosphere;
var subSocket;
var transport = 'long-polling';
// We are now ready to cut the request
var request = { url: 'http://localhost:8080/stream/2',
contentType : "text/plain",
logLevel : 'debug',
transport : transport ,
trackMessageLength : false,
reconnectInterval : 5000,
enableXDR: false,
timeout : 60000,
shared: false
};
request.headers = {"custom-name": "custom-value"
};
request.onOpen = function(response) {
console.log('Atmosphere connected using ' + response.transport)
transport = response.transport;
request.uuid = response.request.uuid;
};
request.onMessage = function(response){
console.log("1")
console.log(response.responseBody);
};
request.onMessagePublished = function(response){
console.log("1")
console.log(response);
};
request.onClose = function(response) {
if (subSocket) {
subSocket.push("Hello");
}
};
request.onError = function(response){
console.log("1")
console.log(response);
};
subSocket = socket.subscribe(request);
如果我使用长轮询onMessage print
org.eclipse.jetty.server.session.HashedSession:1m5nh1kgu8vci1lcp3gmfznozb @ 1801136977
否则如果我使用websocket或JSON都可以正常工作。大气版本2.1.4