如何使用 java 和 dwr 更新 ajax 网络聊天中的用户列表
我正在使用 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.有什么方法可以删除服务器无法向其推送消息的用户吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这里发生的情况是,您正在启动 POST(执行远程方法),但由于它是 onunload,因此浏览器窗口在完成之前就消失了。
我建议尝试 onbeforeunload 而不是 onunload。
关于#2,请查看
DefaultScriptSessionManager
。它具有获取所有活动会话的方法,以及更改超时以检查死会话的方法。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.