WebSocket QML Type

This is a client component for Web Sockets. More...

Import Statement: import WebSockets 1.0

Properties

Signals

Methods

Detailed Description

Use this component to communicate with web socket server. This supports both "ws://" and "wss://" protocols.

Example:

import QtQuick 1.1
import WebSockets 1.0

Rectangle {
    width: 400
    height: 200

    WebSocket {
        id: socket
        // use "wss://echo.websocket.org" for secured socket
        url: "ws://echo.websocket.org"
        onTextMessageReceived: {
            messageBox.text = messageBox.text + "\nReceived message: " + message
        }
        onStatusChanged: {
            if (socket.status === WebSocket.Error) {
                console.log("Error: " + socket.errorString)
            } else if (socket.status === WebSocket.Open) {
                socket.sendTextMessage("Hello World")
            } else if (socket.status === WebSocket.Closed) {
                messageBox.text += "\nSocket closed"
            }
        }
        active: true
    }

    Text {
        id: messageBox
        text: socket.status === WebSocket.Open ? qsTr("Sending...") : qsTr("Welcome!")
        anchors.centerIn: parent
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            socket.active = !socket.active
        }
    }
}

Property Documentation

active : bool

When set to true, a connection is made to the server with the given url. When set to false, the connection is closed. The default value is false.


errorString : string

Contains a description of the last error that occurred. When no error occurrred, this string is empty.


peerAddress : string

Peer IP Address when connected.



Introduced in SDK version: 6


status : Status

Status of the WebSocket.

The status can have the following values:


url : url

Server url to connect to. The url must have one of 2 schemes: ws:// or wss://. When not supplied, then ws:// is used.


Signal Documentation

statusChanged()

This signal is emitted when the status of the WebSocket changes. the status argument provides the current status.

See also WebSocket::status.


textMessageReceived(string message)

This signal is emitted when a text message is received.


Method Documentation

void sendTextMessage(string message)

Sends message to the server.