Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Unable to connect to websocket server on Android QML App



  • Hi,
    I've taken the basic websocket server example from qt5.9, place it in a android project
    and tried to connect to it from a linux based websocket client, but it refuses to accept
    a connection. I see the error in the node console.
    The same app will accept connection if run as a linux desktop app?!?

    On Android i'm using APK19.

    To test if websockets can work in this way on android I've downloaded an
    app from the android store and found I can connect to it fine!

    I checked permissions on the app and found the following:
    ACCESS_NETWORK_STATE
    ACCESS_WIFI_STATE
    CHANGE_NETWORK_STATE
    CHANGE_WIFI_STATE
    INTERNET
    I've added these to the android app, but it still refuses to accept connection!

    Is there a separate permission required for QT/QML network access?
    What would cause this?

    Any help would be appreciated!
    BR
    Paul Grant


  • Qt Champions 2019

    @PaulGrant said in Unable to connect to websocket server on Android QML App:

    I see the error in the node console

    What is the error?



  • HI, error is connection refused, which seems to imply permissions issue!

    Error: connect ECONNREFUSED 192.168.1.141:8003
    at Object.exports._errnoException (util.js:870:11)
    at exports._exceptionWithHostPort (util.js:893:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)

    Additionally if I creat a websocket client in same QML it can communicate
    with the server, even when I change the IP of the server from localhost which is
    default to actual IP of android device. This again seems to imply a permissions issue!?!

    But I dont know of any other relavent Android permissions

    BR, Paul


  • Qt Champions 2019

    @PaulGrant I'm not an Android expert, but I can imagine that Android does not allow connections from outside of the device for security reasons.
    Why would you want to have a server on your phone?


  • Lifetime Qt Champion

    Hi
    Im no expert either but fooling around with my old one and trying stuff i had to root it to connect to it
    https://www.digitaltrends.com/mobile/how-to-make-an-android-server/


  • Moderators

    @PaulGrant
    i am also not an android expert :P
    But following points come to my mind:

    1. are you sure your web-socket server listens on port 8003?
    2. do you let your websocket server listen on QHostAddress::Any network address/interface?
    3. maybe you can try to launch the server only as soon as a network/WiFi connection is available?

    the most important permission is the INTERNET permission, since this lets you open network sockets.



  • You can use OKHTTP3 WebSocket which is really very easy to configure in android you can try this post: https://trinitytuts.com/connect-to-websocket-on-android-using-okhttp/


Log in to reply