码头网络套接字空闲超时

问题描述

带注释的WebSocket在注释中具有自己的超时设置。

@WebSocket(maxIdleTime=30000)

解决方法

我最近一直在使用来处理带注释的Websocket,并与之Jetty API (9.4.5 release)聊天。

但是我遇到了一个问题,5分钟后(我相信这是默认计时器),该会话关闭(这不是由于错误)。我找到的唯一解决方案是在关闭事件发生时通知我的套接字,然后在新的套接字中重新打开连接。

但是我已经阅读了stackOverflow,通过IdleTimeOut在WebsocketPolicy 中进行设置,我可以避免出现此问题:

  1. 我尝试设置3600000为例如,但是行为完全没有改变

  2. 我也尝试将其设置为,-1但出现以下错误:IdleTimeout [-1] must be a greater than or equal to 0

    private ServletContextHandler setupWebsocketContext() {
    ServletContextHandler websocketContext = new AmosContextHandler(ServletContextHandler.SESSIONS |  ServletContextHandler.SECURITY);
    
    WebSocketHandler socketCreator = new WebSocketHandler(){
        @Override
        public void  configure(WebSocketServletFactory factory){    
            factory.getPolicy().setIdleTimeout(-1);
            factory.getPolicy().setMaxTextMessageBufferSize(MAX_MESSAGE_SIZE);
            factory.getPolicy().setMaxBinaryMessageBufferSize(MAX_MESSAGE_SIZE);
            factory.getPolicy().setMaxTextMessageSize(MAX_MESSAGE_SIZE);
            factory.getPolicy().setMaxBinaryMessageSize(MAX_MESSAGE_SIZE);          
            factory.setCreator(new UpgradedSocketCreator());
    
        }
    
    };  
    ServletHolder sh = new ServletHolder(new WebsocketChatServlet());  
    websocketContext.addServlet(sh,"/*");  
    websocketContext.setContextPath("/Chat");
    websocketContext.setHandler(socketCreator);
    websocketContext.getSessionHandler().setMaxInactiveInterval(0);
    return websocketContext;
    

    }

我还尝试OnConnect通过使用call
在事件中直接更改策略session.getpolicy.setIdleTimeOut(),但是我没有注意到任何结果。

这是预期的行为还是我错过了一些东西?谢谢你的帮助。

编辑:

登录关闭:客户端:

2017-07-03T12:48:00.552 DEBUG   HttpClient@179313750-scheduler  Ignored idle endpoint SocketChannelEndPoint@2fb4b627{localhost/127.0.0.1:5080<->/127.0.0.1:53835,OPEN,fill=-,flush=-,to=1/300000}{io=0/0,kio=0,kro=1}->WebSocketClientConnection@e0198ece[ios=IOState@3ac0ec79[CLOSING,in,!out,close=CloseInfo[code=1000,reason=null],clean=false,closeSource=LOCAL],f=Flusher[queueSize=0,aggregateSize=0,failure=null],g=Generator[CLIENT,validating],p=Parser@65c4d838[ExtensionStack,s=START,c=0,len=187,f=null]]

服务器端:

 2017-07-03T12:48:00.595    DEBUG   Idle pool thread    onClose WebSocketServerConnection@e0033d54[ios=IOState@10d40dca[CLOSED,!in,finalClose=CloseInfo[code=1000,clean=true,closeSource=REMOTE],g=Generator[SERVER,p=Parser@317213f3[ExtensionStack,len=2,f=CLOSE[len=2,fin=true,rsv=...,masked=true]]]<-SocketChannelEndPoint@690dfbfb'{'/127.0.0.1:53835<->/127.0.0.1:5080,CLOSED,to=1/360000000}'{'io=0/0,kio=-1,kro=-1}->WebSocketServerConnection@e0033d54[ios=IOState@10d40dca[CLOSED,masked=true]]]
2017-07-03T12:48:00.595 DEBUG   Idle pool thread    org.eclipse.jetty.util.thread.Invocable$InvocableExecutor@4f13dee2 invoked org.eclipse.jetty.io.ManagedSelector$$Lambda$193/682154970@551e133a
2017-07-03T12:48:00.595 DEBUG   Idle pool thread    EatWhatYouKill@6ba355e4/org.eclipse.jetty.io.ManagedSelector$SelectorProducer@7b1559f1/PRODUCING/0/1 produce exit
2017-07-03T12:48:00.595 DEBUG   Idle pool thread    ran EatWhatYouKill@6ba355e4/org.eclipse.jetty.io.ManagedSelector$SelectorProducer@7b1559f1/PRODUCING/0/1
2017-07-03T12:48:00.595 DEBUG   Idle pool thread    run EatWhatYouKill@6ba355e4/org.eclipse.jetty.io.ManagedSelector$SelectorProducer@7b1559f1/PRODUCING/0/1
2017-07-03T12:48:00.595 DEBUG   Idle pool thread    EatWhatYouKill@6ba355e4/org.eclipse.jetty.io.ManagedSelector$SelectorProducer@7b1559f1/PRODUCING/0/1 run
2017-07-03T12:48:00.597 DEBUG   Idle pool thread    127.0.0.1 has disconnected !

2017-07-03T12:48:00.597 DEBUG   Idle pool thread    Disconnected:  127.0.0.1 (127.0.0.1)  (statusCode= 1,000,reason=null)