我需要注册用户从我的 JEE Web 应用程序注销的时间,可以手动使用注销按钮,也可以关闭用户打开的所有窗口(浏览器)。
登录/注销使用 CDI 会话 bean 进行管理。
这是我到目前为止所尝试的:
$(window).on('beforeUnloadEvent', function() {
$.ajax({
url: '/registerlogout',
type: 'POST'
});
});
@Named
@SessionScoped
public class LoginController implements Serializable{
public void login(){
...
saveUserLoginTime();
...
}
public void logout(){
...
saveUserLogoutTime();
...
}
@Predestroy
public void onclose(){
saveUserLogoutTime();
}
}
function createWebSocket() {
var socket = new WebSocket('wss://your-websocket-url');
socket.onopen = function() {
console.log('WebSocket connection established.');
// Perform any necessary actions when the connection is opened
};
socket.onclose = function(event) {
console.log('WebSocket connection closed with code: ' + event.code);
handleWebSocketClose();
};
}
function handleWebSocketClose() {
//ajax call to register userlogouttime
}
createWebSocket();
@WebListener
public class SessionListener implements HttpSessionListener{
@Override
public void sessionCreated(HttpSessionEvent e){
...
}
@Override
public void sessionClosed(HttpSessionEvent e){
System.out.println("Session close: " + e.getSession().getId());
}
}
我认为解决方案可能是某种捕捉会话结束事件或类似的东西。这样的事件类似于用 HttpSessionListener 触发的会话销毁。我没有想法,希望你能帮忙。