tags:

views:

38

answers:

1

I am developing a web chat with java and dwr ajax reverse. I've got two questions on how to remove users when they are offline

1.when user close the browser

server side code 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);
            }
        });
    }
}

client side code javascript

function removeUser(){
     Chat.addOrRemoveUser(chat.userid,0);
}

i bind removeUser() with onunload event when user close the browser. it is ok with firefox and chrome but failed in ie8 with the following server log :

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.Is there any way to remove the users who the server can not push message to ?

A: 

I think what's happening here is that you're starting a POST (executing your remoted method), but since it's onunload, the browser window goes away before it's complete.

I'd suggest trying onbeforeunload instead of onunload.

Regarding #2, take a look at DefaultScriptSessionManager. This has methods to get all active sessions, as well as changing the timeout to check for dead sessions.

desau