我正在开发一个与java和dwr ajax反向的网络聊天。关于如何在用户离线时删除用户,我有两个问题
1.当用户关闭浏览器时
服务器端代码java
import java.util.HashMap;
import java.util.Map;
import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSessions;
public class Chat{
private int id = 0;
private final Map<String,String> users = new HashMap<String, String>();
/**
* @param idOrName when type=0 is id when type=1 is name
* @param type type=0 remove a user type=1 add a user
*/
public void addOrRemoveUser(String idOrName,int type){
if(type == 0){
users.remove(idOrName);
}
if(type == 1){
users.put(String.valueOf(id++),idOrName);
}
Browser.withCurrentPage(new Runnable() {
public void run() {
ScriptSessions.addFunctionCall("updateUserList", users);
}
});
}
}
客户端代码javascript
function removeUser(){
Chat.addOrRemoveUser(chat.userid,0);
}
当用户关闭浏览器时,我将removeUser()绑定到onunload事件。它可以使用firefox和chrome,但在ie8中使用以下服务器日志失败:
2010-8-10 22:34:58 org.directwebremoting.dwrp.BaseCallHandler marshallException
警告: Exception while processing batch
org.directwebremoting.extend.ServerException: Failed to read input
at org.directwebremoting.dwrp.Batch.parseBasicPost(Batch.java:224)
at org.directwebremoting.dwrp.Batch.parsePost(Batch.java:116)
at org.directwebremoting.dwrp.Batch.<init>(Batch.java:56)
at org.directwebremoting.dwrp.CallBatch.<init>(CallBatch.java:46)
at org.directwebremoting.dwrp.BaseCallHandler.handle(BaseCallHandler.java:72)
at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:120)
at org.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:141)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:163)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:556)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:402)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:310)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:575)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1555)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.io.IOException
at org.apache.coyote.http11.InternalAprInputBuffer.fill(InternalAprInputBuffer.java:798)
at org.apache.coyote.http11.InternalAprInputBuffer$SocketInputBuffer.doRead(InternalAprInputBuffer.java:827)
at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:116)
at org.apache.coyote.http11.InternalAprInputBuffer.doRead(InternalAprInputBuffer.java:738)
at org.apache.coyote.Request.doRead(Request.java:427)
at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:286)
at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java:407)
at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:309)
at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:198)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at org.directwebremoting.dwrp.Batch.parseBasicPost(Batch.java:181)
... 23 more
2.有没有办法删除服务器无法将消息推送到的用户?
答案 0 :(得分:0)
我认为这里发生的事情是你正在开始一个POST(执行你的远程方法),但由于它是onunload,浏览器窗口会在它完成之前消失。
我建议尝试onbeforeunload而不是onunload。
关于#2,请查看DefaultScriptSessionManager
。这有获取所有活动会话的方法,以及更改超时以检查死会话。