Git Product home page Git Product logo

Comments (5)

andreasley avatar andreasley commented on May 18, 2024 6

@tannernelson @loganwright
Using while like this will block a GCD thread permanently.
Better use a DispatchSource:

var timer = DispatchSource.makeTimerSource()
let interval = DispatchTimeInterval.seconds(10)
timer.scheduleRepeating(deadline: .now() + interval, interval: interval, leeway: .seconds(1))
timer.setEventHandler { [weak ws, weak timer] in
    guard ws?.state == .open else {
        timer?.cancel()
        timer = nil
        return
    }
    print("ping")
    try? ws?.ping()
}
timer.resume()

// `timer` needs to be retained as long as the WebSocket lives

from http.

loganwright avatar loganwright commented on May 18, 2024

@PorterHoskins until we implement this internally, you can use the code snippet to keep your ws alive.

from http.

AlexKel avatar AlexKel commented on May 18, 2024

@tannernelson are you sure this snippet is right?

try background { [weak ws] in
        if ws?.state == .open {
            print("ping")
            try? ws?.ping()
            drop.console.wait(seconds: 10)
        }
    }

This seems to ping it only once. Shouldn't it be while ?

from http.

loganwright avatar loganwright commented on May 18, 2024

@AlexKel yes, yes it should. Thanks for that, for anyone who wants to see how that looks:

    try background { [weak ws] in
            while ws?.state == .open {
                print("ping")
                try? ws?.ping()
                drop.console.wait(seconds: 10)
            }
    }

from http.

tanner0101 avatar tanner0101 commented on May 18, 2024

Oops, yeah I meant to write while ws?.state == .open

from http.

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.