Spring websocket timeout settings

I am using Spring websocket support. My question is how to set the web connection timeout. The connection now closes automatically after a few minutes. I want the connection to never close.

Here is my websocket handler:

public class MyHandler implements WebSocketHandler {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    class MyTimerTask extends TimerTask {
        private WebSocketSession session;
        public MyTimerTask(WebSocketSession session) {
            this.session = session;
        }

        @Override
        public void run() {
            try {
                String msg = ((int)(Math.random()*50)) + "";
                this.session.sendMessage(new TextMessage(msg.toString()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Autowired
    private UserDao userDao;

    @Autowired
    private JdbcDaoImpl jdbcDaoImpl;
    private Timer timer;

    @Override
    public void afterConnectionEstablished(WebSocketSession session)
            throws Exception {
        System.out.println("websocket????");
        timer = new Timer();
        timer.schedule(new MyTimerTask(session), 0, 1000);
        logger.info("logger connection");
    }

    @Override
    public void handleMessage(WebSocketSession session,
            WebSocketMessage<?> message) throws Exception { }

    @Override
    public void handleTransportError(WebSocketSession session,
            Throwable exception) throws Exception { }

    @Override
    public void afterConnectionClosed(WebSocketSession session,
            CloseStatus closeStatus) throws Exception {
        System.out.println("websocket????");
        timer.cancel();
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }   
}

      

my websocket config:

<websocket:handlers>
    <websocket:mapping path="/myHandler" handler="myHandler"/>
</websocket:handlers>

<bean id="myHandler" class="com.sdp.websocket.MyHandler"/>

      

and the javascript client:

var webserver = 'ws://localhost:8080/authtest/myHandler';
var websocket = new WebSocket(webserver);
websocket.onopen = function (evt) { onOpen(evt) }; 
websocket.onclose = function (evt) { onClose(evt) }; 
websocket.onmessage = function (evt) { onMessage(evt) }; 
websocket.onerror = function (evt) { onError(evt) }; 

function onOpen(evt) { 
    console.log("Connected to WebSocket server."); 
} 

function onClose(evt) { 
    console.log("Disconnected"); 
} 

function onMessage(evt) { 
    console.log('Retrieved data from server: ' + evt.data); 
} 

function onError(evt) { 
    console.log('Error occured: ' + evt.data); 
}
debugger;
function sendMsg(){
    websocket.send("{msg:'hello'}");
}

      

+5


source to share


1 answer


The web socket remains open until the server or client decides to close it. However, websockets are affected by two timeouts:

  • HTTP session timeout;
  • timeouts for connecting to a proxy;

If all you have between your client and your server is a web socket connection and you are not communicating over HTTP with AJAX or requesting other pages, the HTTP session will expire and some servers decide to invalidate it along with the web socket (there was a bug in Tomcat7 it did just that). Some other servers don't do this because they see activity on the websocket. See here for an extended discussion: Need some permission or clarification on how and when the HttpSession last access time is updated .



Another timeout with proxy. They see the connection, and if there is no activity on the wire for a long period of time, they simply cut it off because they think it is stuck. To solve this problem, while you are not sending the actual application data, you need to pulse or ping messages from time to time so the proxy knows the connection is still ok.

Other issues may also occur, such as erroneous browser support for websocket, your network settings, firewall rules, etc.

For available timeout options in Spring, see the websocket documentation: Configuring the WebSocket Engine .

+13


source







All Articles