嗨,我正在尝试从 [http://code.google.com/p/jquery-stream/wiki/ChatExample][1] 实现 java servlet 3.0 聊天示例。它在本地主机上运行正常,但是当上传到远程服务器时,流不会打开/启动或发送消息。我错过了什么?请帮忙。
<!--client side-->
<html>
<head>
<title>Chat - Servlet 3</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.8.2.js"></script>
<script type="text/javascript" src="jquery.stream-1.2.js"></script>
<script type="text/javascript">
$.stream.setup({enableXDR: true});
var chat = {
lastUsername: "Donghwan Kim",
username: $.trim(window.prompt("Username?")) || "Anonymous" + $(window).width()
};
$(function() {
$.stream("chat", {
type: "http",
dataType: "json",
context: $("#content")[0],
open: function(event, stream) {
stream.send({username: chat.username, message: "Hello"});
},
message: function(event) {
if (chat.lastUsername !== event.data.username) {
$("<p />").addClass("user").text(chat.lastUsername = event.data.username).appendTo(this);
}
$("<p />").addClass("message").text(event.data.message).appendTo(this);
this.scrollTop = this.scrollHeight;
}
});
$("#editor .user").text(chat.username);
$("#editor .message").keyup(function(event) {
if (event.which === 13 && $.trim(this.value)) {
$.stream().send({username: chat.username, message: this.value});
this.value = "";
}
});
});
</script>
</head>
<body>
<div id="content" class="content">
<p class="user"></p>
<p class="message"></p>
</div>
<div id="editor" class="editor">
<p class="user"></p>
<form action="#" onsubmit="return false;">
<input class="message" type="text" />
</form>
</div>
</body>
</html>
//server side
package com.chatty;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/chat", asyncSupported = true)
public class ChatServlet extends HttpServlet {
private static final long serialVersionUID = -277914015930424042L;
private Map<String, AsyncContext> asyncContexts = new ConcurrentHashMap<String, AsyncContext>();
private BlockingQueue<String> messages = new LinkedBlockingQueue<String>();
private Thread notifier = new Thread(new Runnable() {
public void run() {
while (true) {
try {
// Waits until a message arrives
String message = messages.take();
// Sends the message to all the AsyncContext's response
for (AsyncContext asyncContext : asyncContexts.values()) {
try {
sendMessage(asyncContext.getResponse().getWriter(), message);
} catch (Exception e) {
asyncContexts.values().remove(asyncContext);
}
}
} catch (InterruptedException e) {
break;
}
}
}
});
private void sendMessage(PrintWriter writer, String message) throws IOException {
// default message format is message-size ; message-data ;
writer.print(message.length());
writer.print(";");
writer.print(message);
writer.print(";");
writer.flush();
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
notifier.start();
}
// GET method is used to establish a stream connection
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Content-Type header
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
// Access-Control-Allow-Origin header
response.setHeader("Access-Control-Allow-Origin", "*");
PrintWriter writer = response.getWriter();
// Id
final String id = UUID.randomUUID().toString();
writer.print(id);
writer.print(';');
// Padding
for (int i = 0; i < 1024; i++) {
writer.print(' ');
}
writer.print(';');
writer.flush();
final AsyncContext ac = request.startAsync();
ac.addListener(new AsyncListener() {
public void onComplete(AsyncEvent event) throws IOException {
asyncContexts.remove(id);
}
public void onTimeout(AsyncEvent event) throws IOException {
asyncContexts.remove(id);
}
public void onError(AsyncEvent event) throws IOException {
asyncContexts.remove(id);
}
public void onStartAsync(AsyncEvent event) throws IOException {
}
});
asyncContexts.put(id, ac);
}
// POST method is used to communicate with the server
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
AsyncContext ac = asyncContexts.get(request.getParameter("metadata.id"));
if (ac == null) {
return;
}
// close-request
if ("close".equals(request.getParameter("metadata.type"))) {
ac.complete();
return;
}
// send-request
Map<String, String> data = new LinkedHashMap<String, String>();
data.put("username", request.getParameter("username"));
data.put("message", request.getParameter("message"));
try {
messages.put(new Gson().toJson(data));
} catch (InterruptedException e) {
throw new IOException(e);
}
}
@Override
public void destroy() {
messages.clear();
asyncContexts.clear();
notifier.interrupt();
}
}