Skip to content

An implementation of QWebSocket that behaves similar to a SocketIO client without needing an additional library. It works well in QML.

License

Notifications You must be signed in to change notification settings

Dimcon/ParthanonsQWebSocketIO

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Easy ParthanonsQWebSocketIO

An implementation of QWebSocket that behaves similar to a SocketIO client without needing an additional library. It works well in QML.

It will retry to connect if a connection fails. It buffers outbound messages for 5 connection attempts if it fails to send. (can be changed with retryLimit variable)

It still emits the error signal from the QWebSocket, but will catch the error and try to reconnect afterwards.

Usage:

Add the class to your QML:

QGuiApplication app(argc, argv);
qmlRegisterType<ParthanonsQWebSocket>("ParthanonsQWebSocket",1,0,"ParthanonsQWebSocket");
const QUrl url(QStringLiteral("qrc:/main.qml"));
QQmlApplicationEngine engine;
engine.load(url);
return app.exec();

Use within QML main.qml:

ParthanonsQWebSocket {
    id: socket
    Component.onCompleted: {
        socket.on("RecieveTest", function(data) {
            console.log("GOT DATA FROM SERVER: " + data)
            let obj = JSON.parse(data);
            console.log(obj)
        })
        socket.connectTo("ws://demos.kaazing.com/echo")
    }
    onConnectionEstablished: {
        // Do connection established stuff
        statusIndicator.state = "Connected"
    }
    onDisconnect: {
        // Do Connection died stuff
        statusIndicator.state = "Disconnected"
    }
}

Item {
    MouseArea {
        onClicked: {
            data = {
                mydata: "data", 
                moredata: "moredata"
            }
            socket.send("ServerEndpoint", JSON.stringify(data), (result)=>{
                if (result == "Failed"){
                        console.log("Send timed out.")
                } else {
                        console.log("Message sent")
                }
            })
        }
    }
}

About

An implementation of QWebSocket that behaves similar to a SocketIO client without needing an additional library. It works well in QML.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages