在聊天小程序上注销用户时出现问题
我希望 Java 聊天小程序的用户在关闭浏览器窗口时自动注销。
我目前使用以下方法:
public void destroy() {
sendLogoutMessage();
}
然而,这只在 3/4 的时间内有效(可能是由于网络延迟)。
聊天小程序 ping 服务器并在 90 秒后将它们注销(这允许它们由于任何互联网问题而重新连接) - 所以它们最终会被删除,但是我想要一种更好地捕获关闭事件的方法。
I wish for users of a Java chat applet to be automatically logged out when closing the browser window.
I currently use the following:
public void destroy() {
sendLogoutMessage();
}
However this only works 3/4 of the time (probably due to network delays).
The chat applet pings the server and logs them out after 90 seconds (this allows them to reconnect due to any internet problems) - so they do get removed eventually, however I would like a way to better catch the close event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的代码在 100% 的情况下不起作用,因为它是在 destroy 方法中调用的,该方法不会等待并关闭所有内容,包括打开的网络连接。因此,有时如果网络速度较慢,小程序及其输出流会在向服务器发送注销消息之前被终止。
如果我的理论是正确的,你也许可以检查一下。
另外我建议如下。
该解决方案应该是对您现有解决方案的补充,其工作效率为 3/4。
我希望这有帮助。祝你好运。
I think that your code does not work in 100% cases because it is called in destroy method that does not wait and closes everything including network connections being opened. So, sometimes if network is slow the applet together with its output streams is killed before it sends the logout message to server.
If my theory is correct you can probably check it.
Additionally I'd suggest the following.
This solution should be an addition to your existing solution that works 3/4 of the times.
I hope this helps. Good luck.