How to check whether a port is open at client's network/firewall?

前端 未结 8 1417
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 13:13

This is solved at last with \"timeout\" attribute of jQuery AJAX (and JSONP). See my own answer !

Please see the updated part, I

相关标签:
8条回答
  • 2020-11-30 13:42

    I am occasional frontend/javascript/jQuery guy, so this may not be 100% professional, but it is good enough and it solved my similar problem:

    ping_desktop_app = $.get({
      url: "http://127.0.0.1:#{desktop_app_port}",
      dataType: 'jsonp',
    })
    
    $(@).parent().find(".click-me-to-use-desktop-app").click ->
      if ping_desktop_app.status == 200
        $.get({
          url: "http://127.0.0.1:#{desktop_app_port}/some_command/123123",
          dataType: 'jsonp',
        })
      else
        alert("Please run your desktop app and refresh browser")
    

    I could not check whether port is open (desktop app is running) on server side because views are cached, so I needed to check the localhost/port right before user click in browser

    Edits translating to JS are welcome

    0 讨论(0)
  • 2020-11-30 13:45

    I don't think you understand the use cases for JSONP and it's not possible to test open ports with it. http://en.wikipedia.org/wiki/JSONP

    If you want a client side solution it could be possible with websockets, but this is only available on new browsers like chrome or ff. Otherwise request a server side script which does the ping. For example - with a curl script: curl and ping - how to check whether a website is either up or down?

    0 讨论(0)
提交回复
热议问题