Java 网箱的延迟断开机构
我正在尝试为我正在开发的WebSocket聊天添加延迟断开连接机制。这意味着,如果用户断开连接,但在某个时间限制内重新连接(我将使用30秒作为示例),则断开会被忽略。这样做的原因是,如果用户短暂失去连接,例如,移动用户进入电梯,这是一个概念证明 我决定用饼干来做这个。我得出的逻辑是,当打开WebSocket时,它也会打开一个HttpSession。由此,我可以检查是否存在具有特定id的cookie。如果是这样,那么他们就不会被视为新用户。但是,为此,我需要能够在套接字关闭后将cookie的过期时间设置为30秒 我已经知道这可以做到,但是当我在服务器上的OnClose方法中尝试此操作时,服务器抛出了一个NullPointerException。这并不奇怪,因为我显然是在用户会话关闭后试图访问它 那么,有没有办法做到这一点 2月16日更新我决定在发送消息时尝试完全重置cookie。这部分是可行的,因为cookies是生成并添加到HttpSession的,但是在重新连接服务器时,服务器认为用户是全新的。所以,我认为我的问题是cookie没有被发送给用户 更新2阅读后,我将cookie生成移动到一个配置类中,该类在成功握手时调用。如果请求没有cookie,它将被视为一个全新的连接,并将其记录到系统控制台作为概念证明。我必须做的一件事是从一开始就延长饼干的寿命:目前,大概是10分钟。如果我不知道如何做到我上面所说的,我就这么做Java 网箱的延迟断开机构,java,cookies,websocket,reconnect,Java,Cookies,Websocket,Reconnect,我正在尝试为我正在开发的WebSocket聊天添加延迟断开连接机制。这意味着,如果用户断开连接,但在某个时间限制内重新连接(我将使用30秒作为示例),则断开会被忽略。这样做的原因是,如果用户短暂失去连接,例如,移动用户进入电梯,这是一个概念证明 我决定用饼干来做这个。我得出的逻辑是,当打开WebSocket时,它也会打开一个HttpSession。由此,我可以检查是否存在具有特定id的cookie。如果是这样,那么他们就不会被视为新用户。但是,为此,我需要能够在套接字关闭后将cookie的过期时
更新日期:2月19日我已经完全放弃了饼干。查看我的解决方案。我通过完全放弃cookies解决了这个问题。我刚刚展示了相关类中的方法;如果这还不够,我将编辑我的答案以包含完整的代码 在configuration类中,我得到请求的x-forwarded-for头。这与客户端的IP地址相匹配,尤其是因为我的后端服务器位于代理服务器后面。如果用户的IP地址在用户列表中,则刷新其连接;否则,它们将添加到列表中。断开连接后,无论出于何种原因,用户都会被标记为断开连接 一个单独的ConnectionMonitor类实现Runnable接口,每10秒运行一次,并检查是否有任何客户端已断开连接超过30秒。如果已删除,则会将其从用户列表中删除 MyConfigClass.modifyHandshake ConnectionMonitor.updateUsers在单独的线程中运行
void updateUsers()
{
for(String id : ChatroomServerEndpoint.users.keySet())
{
User theUser = ChatroomServerEndpoint.users.get(id);
if (theUser.getStatus() == User.Connection.DISCONNECTED)
{
// get the time at which the user disconnected
Calendar disconnectDate = theUser.getdisconnectionDate();
// Calendar.getTime.getTime returns milliseconds,
// so, multiply maxDisconnectTime by 1000 to see if the user has expired
if (theDate.getTime().getTime() - disconnectDate.getTime().getTime()
>= maxDisconnectTime * 1000 )
{
System.out.println(id + " has timed out");
ChatroomServerEndpoint.users.remove(id);
}
}
}
}
使用者
}
void updateUsers()
{
for(String id : ChatroomServerEndpoint.users.keySet())
{
User theUser = ChatroomServerEndpoint.users.get(id);
if (theUser.getStatus() == User.Connection.DISCONNECTED)
{
// get the time at which the user disconnected
Calendar disconnectDate = theUser.getdisconnectionDate();
// Calendar.getTime.getTime returns milliseconds,
// so, multiply maxDisconnectTime by 1000 to see if the user has expired
if (theDate.getTime().getTime() - disconnectDate.getTime().getTime()
>= maxDisconnectTime * 1000 )
{
System.out.println(id + " has timed out");
ChatroomServerEndpoint.users.remove(id);
}
}
}
}
public class User {
// the ID is the user's IP address
private String id;
// connection status
public enum Connection
{
CONNECTED,
DISCONNECTED
}
private Connection status;
// the time of disconnection
private Calendar disconnectionDate;
// each user needs a WebSocket Session to be able to send and receive messages
private Session userSession;
/**
* @return the id of this user
*/
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* @return connection status
*/
public Connection getStatus() {
return status;
}
public void setStatus(Connection status) {
this.status = status;
}
public Calendar getdisconnectionDate() {
return disconnectionDate;
}
public void setdisconnectionDate(Calendar disconnectionDate) {
this.disconnectionDate = disconnectionDate;
}
/**
* @return the userSession
*/
public Session getUserSession() {
return userSession;
}
/**
* @param userSession the userSession to set
*/
public void setUserSession(Session userSession) {
this.userSession = userSession;
}
/**
* @param newID the new ID of the user
*/
public User (String newID)
{
this.id = newID;
this.status = Connection.CONNECTED;
}
/**
* Toggles the connection
* @param toggle - if true, the user is connected
*/
public void toggleConnection(boolean toggle)
{
if (toggle == false)
{
status = Connection.DISCONNECTED;
disconnectionDate = Calendar.getInstance();
}
else
{
status = Connection.CONNECTED;
disconnectionDate = Calendar.getInstance();
disconnectionDate.add(Calendar.HOUR, 1); // give an extra hour to prevent them being disconnected too soon
}
}