简体   繁体   English

如何使用Java和DWR更新Ajax网络聊天中的用户列表

[英]how to update user list in a ajax web chat using java and dwr

I am developing a web chat with java and dwr ajax reverse. 我正在使用Java和dwr ajax reverse开发网络聊天。 I've got two questions on how to remove users when they are offline 关于用户离线时如何删除用户,我有两个问题

1.when user close the browser 1.当用户关闭浏览器时

server side code java 服务器端代码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 客户端代码javascript

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

i bind removeUser() with onunload event when user close the browser. 当用户关闭浏览器时,我将removeUser()与onunload事件绑定。 it is ok with firefox and chrome but failed in ie8 with the following server log : 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.Is there any way to remove the users who the server can not push message to ? 2.是否可以删除服务器无法向其发送消息的用户?

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. 我认为这里正在发生的事情是您正在启动POST(执行远程方法),但是由于它处于onunload状态,因此浏览器窗口在完成之前就消失了。

I'd suggest trying onbeforeunload instead of onunload. 我建议尝试onbeforeunload而不是onunload。

Regarding #2, take a look at DefaultScriptSessionManager . 关于#2,请看一下DefaultScriptSessionManager This has methods to get all active sessions, as well as changing the timeout to check for dead sessions. 它具有获取所有活动会话以及更改超时以检查死会话的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM