我正在使用 java 和 dwr ajax reverse 开发网络聊天。我有两个关于如何在用户离线时删除用户的问题
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.有什么办法可以去掉服务器不能推送消息的用户?