我正在尝试运行我通过netbeans创建的网站,同时使用chrome和firefox。我有一个多人迷宫游戏,当我移动时,服务器发送我移动的其他玩家。它适用于前3个动作,然后返回ERR_EMPTY_RESPONSE
。我尝试清除历史记录,隐身模式重启以及我在网络上找到的许多其他解决方案。
这是我的JS代码:
function opponentMove() {
var url = "/AsyncMulti?sendToServer=" + sendToServer;
$.ajax({
url: url,
method: 'GET',
//dataType: 'json',
contentType: "application/json; charset=utf-8",
cache: false,
success: function (data) {
//parseData(data);
sendToServer = "wait for play";
moveOther((data.maze).toString());
setTimeout(
opponentMove, /* Request next opponent move */
500 /* ..after .5 seconds */
);
},
error: function (XMLHttpRequest, textStatus, errorThrown){
setTimeout(
opponentMove, /* Try again after.. */
1000); /* milliseconds (1 second) */
}
});
}
java代码:
@WebServlet(name = "AsyncMulti",
asyncSupported = true,
urlPatterns = {"/AsyncMulti"})
public class AsyncMulti extends HttpServlet {
Enumeration<String> e;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
SessionDB c = SessionDB.getInstance();
System.out.println("LongPollingServlet.doGet()");
String s = req.getSession().getId();
MultiPlayer m = c.Socket(req.getSession().getId());
e = req.getParameterNames();
m.SetSendMessae(req.getParameter("sendToServer"));
boolean asyncSupported = req.isAsyncSupported();
System.out.println("asyncSupported 1: " + asyncSupported);
// Tomcat only
req.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
asyncSupported = req.isAsyncSupported();
System.out.println("asyncSupported 2: " + asyncSupported);
if (asyncSupported) {
AsyncContext asyncCtx = req.startAsync(req, res);// req.startAsync();
asyncCtx.setTimeout(0); // => disable timeout
AsyncRunnable thread = new AsyncRunnable(asyncCtx);
asyncCtx.start(thread);
}
}
}
class AsyncRunnable implements Runnable {
private final AsyncContext asyncContext;
public AsyncRunnable(AsyncContext asyncContext) {
this.asyncContext = asyncContext;
}
@Override
public void run() {
StringBuilder sb = new StringBuilder();
HttpServletRequest req = (HttpServletRequest) asyncContext.getRequest();
SessionDB c = SessionDB.getInstance();
HttpServletResponse res = (HttpServletResponse) asyncContext.getResponse();
PrintWriter pw = null;
MultiPlayer m = c.Socket(req.getSession().getId());
JSONObject obj = new JSONObject();
String s = m.getRecieveFromServer();
int i;
if (c.isMulty && !s.contains("right") && !s.contains("left") &&
!s.contains("down") && !s.contains("up"))
sb.append('p');
if( s.contains("right") || s.contains("left") ||
s.contains("down") || s.contains("up"))
sb.append(s);
if (s.contains("pla"))
c.setFlag();
if (s.contains("Maze")){
i = s.indexOf("Maze");
for (i += 7; i < s.length(); i++) {
if (s.charAt(i) == '"') {
break;
}
sb.append(s.charAt(i));
}
i = s.indexOf("Maze", i);
// sb.append('*');
for (i += 7; i < s.length(); i++) {
if (s.charAt(i) == '"') {
break;
}
sb.append(s.charAt(i));
}
}
HttpServletResponse peer = (HttpServletResponse) asyncContext.getResponse();
try {
try {
obj.put("maze", sb);
} catch (JSONException ex) {
Logger.getLogger(AsyncRunnable.class.getName()).log(Level.SEVERE, null, ex);
}
peer.getWriter().write(obj.toString());
} catch (IOException ex) {
Logger.getLogger(AsyncRunnable.class.getName()).log(Level.SEVERE, null, ex);
}
res.setContentType("application/json");
asyncContext.complete();
System.out.println("After calling complete");
}
}