Code Monkey home page Code Monkey logo

Comments (7)

davidepianca98 avatar davidepianca98 commented on May 25, 2024 1

Indeed changing the TCP connect timeout is not easy, especially considering the many platforms to support. But it is something I would like to implement in the future if possible, so I'll keep the issue open for now.

from kmqtt.

contactgc avatar contactgc commented on May 25, 2024 1

@davidepianca98 I've taken some time to work on that and I found a way... And it wasn't so difficult.

in the ClientSocket.kt (in jvmMain) this little change make the job:

public actual class ClientSocket actual constructor(
    address: String,
    port: Int,
    maximumPacketSize: Int,
    private val readTimeOut: Int,
    checkCallback: () -> Unit
) : Socket(
    SocketChannel.open(), //delay the connection by ignoring the connect information in the constructor
    null,
    ByteBuffer.allocate(maximumPacketSize),
    ByteBuffer.allocate(maximumPacketSize)
) {

    private val selector = Selector.open()

    init {
        channel.socket().connect(InetSocketAddress(address, port), 5000)  //connect with a timeout parameter (nedd to be moved to the constructor parameters
        channel.configureBlocking(false)
        channel.register(selector, SelectionKey.OP_READ)

  //handle the timeout with an exception
        if (!channel.isConnected()) {
            throw Exception("Custom Timeout")
        }
    }

This is a POC implementation but I hope it will help you in your work.

Cheers

from kmqtt.

davidepianca98 avatar davidepianca98 commented on May 25, 2024

Hello, thank you for the suggestion! Do you mean the TCP connection timeout or MQTT CONNACK response timeout?
The former is platform specific and probably a bit harder. The latter is very simple. I would like to add both but I am not able to give a timeline.

from kmqtt.

davidepianca98 avatar davidepianca98 commented on May 25, 2024

The CONNACK received timeout has been added here c97c4f9

from kmqtt.

contactgc avatar contactgc commented on May 25, 2024

Thanks that's great and quick!

In fact I was thinking of the TCP timeout.
My use case is:

  • Client start and try to connect before the Broker is live and I would like to change the timeout so I can retry to connect.
    When trying this test I found a consistent timeout of 130 seconds. I'll let you know here if I've found a way to deal with this.

--
edit: The connection timeout came from SocketChannel.open(InetSocketAddress(address, port)) and from what I've seen so far it's not that easy to change the timeout.

Anyway thank for your help!

from kmqtt.

davidepianca98 avatar davidepianca98 commented on May 25, 2024

Thanks a lot! I will definitely use this when I get to implement this. Now only the POSIX and JS implementation are needed.

from kmqtt.

contactgc avatar contactgc commented on May 25, 2024

Yes, sure, good luck on that ;)

from kmqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.