Skip to content

Timeout in WebSocketClient not used #177

Closed
@sfroeth

Description

@sfroeth

Hi,

One of the constructors of WebSocketClient takes a value for a timeout, but it is actually never used. The default timeout used by SocketChannel.connect() seems to be something around two minutes if the remote host does not answer. For my use case much shorter timeouts are necessary. I fixed it for me using a thread and calling SocketChannel.close():

// method interruptibleRun():

        /*
         *  Timeout for the SocketChannel using InterruptibleChannel.close()
         *  SocketChannel.connect(...) throws AsynchronousCloseException after calling close()
         */
        if(timeout != 0) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        Thread.sleep(timeout);
                        if(!channel.isConnected()) {
                            channel.close();
                        }
                    } catch (Exception e) {
                        // does not matter
                    }
                }
            }).start();
        }

        channel.connect( new InetSocketAddress( host, port ) );

Regards
Stefan

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions