WebSocketServer QML Type

WebSocket Server component. More...

Import Statement: import WebSockets 1.0

Properties

Signals

Detailed Description

The app can use this component to run a WebSocket server.

Example:

import QtQuick 1.1
import com.dolby.dcp.components 1.0
import com.dolby.dcp.engine 1.0
import WebSockets 1.0

Rectangle {
    id: wsRoot
    width: 360
    height: 360

    /////////////////////////
    // server
    /////////////////////////
    property bool clientConnected: false
    property WebSocket wsClient: null

    // Connections to the client
    Connections {
        target: (wsRoot.wsClient) ? wsRoot.wsClient : null
        onStatusChanged: {
            // Note: always use the 'status' property, not the argument from the signal.
            Logger.syslog(LogLevel.INFO, "Server: Client status : " + wsRoot.wsClient.status);
            if (wsRoot.wsClient.status === WebSocket.Error) {
                wsRoot.appendMessage("Server: Client error:" + wsRoot.wsClient.errorString);
            } else if (wsRoot.wsClient.status === WebSocket.Closing) {
                wsRoot.appendMessage("Server: Client socket closing.");
            } else if (wsRoot.wsClient.status === WebSocket.Closed) {
                wsRoot.appendMessage("Server: Client socket closed.");
                Logger.syslog(LogLevel.INFO, "Server: Client socket closed! ");
                wsRoot.clientConnected = false;
                if(wsRoot.wsClient) wsRoot.wsClient = null;
            }
        }
        onTextMessageReceived: {
            wsRoot.appendMessage("Server received message: " + message);
            wsRoot.wsClient.sendTextMessage("Server Echo: " + message);
        }
        onActiveChanged: {
            wsRoot.appendMessage("Server: Client connection active: " + wsRoot.wsClient.active);
        }
    }


    WebSocketServer {
        id: server
        port:12350
        host: configuration.getParameter("Network.IpAddress").value
        listen: true
        secure: false // set true for secure connection
        onClientConnected: {
            Logger.syslog(LogLevel.INFO, "WS Client connected!")
            wsRoot.appendMessage("Server: Client status: " +  webSocket.status)
            if(webSocket.peerAddress !== configuration.getParameter("Dvms.PeerDevice.IpAddress").value) {
                wsRoot.appendMessage("Server: Unknown Client connected, closing connection " +  webSocket.peerAddress);
                Logger.syslog(LogLevel.INFO, "Server: Unknown Client connected, closing connection " + webSocket.peerAddress);

                // Disconnect
                webSocket.url = "";
            }
            else if (wsRoot.clientConnected) {
                wsRoot.appendMessage("Server: Second client connected, closing connection " +  webSocket.peerAddress);
                Logger.syslog(LogLevel.INFO, "Server: Second Client connected, closing connection " + webSocket.peerAddress);
                // Disconnect
                webSocket.url = "";
            }
            else {
                wsRoot.appendMessage("Server: Client connected, accept connection " +  webSocket.peerAddress);
                Logger.syslog(LogLevel.INFO,"Server: Client connected, accept connection " +  webSocket.peerAddress)

                wsRoot.clientConnected = true;
                wsRoot.wsClient = webSocket;
            }
        }

        onErrorStringChanged: {
            wsRoot.appendMessage("Server error: "+ errorString);
        }
        Component.onCompleted: {
            wsRoot.appendMessage("Server URL: " + url);
            // activate the client connection
            clientSocket.url = url;
        }
    }

    /////////////////////////
    // client
    /////////////////////////
    // Demo client (typically on different machine)
    WebSocket {
        id: clientSocket
        onTextMessageReceived: wsRoot.appendMessage("Client: received message: " + message)
        onStatusChanged: {
            if (clientSocket.status == WebSocket.Error) {
                wsRoot.appendMessage("Client: connection error: "+ clientSocket.errorString);
            } else if (clientSocket.status == WebSocket.Closed) {
                wsRoot.appendMessage("Client: socket closed.");
            }
        }
    }

    /////////////////////////
    // message box
    /////////////////////////
    function appendMessage(message) {
        messageBox.text += "\n" + message
    }

    Text {
        id: messageBox
        text: "Click to send a message!"
        anchors.fill: parent
        wrapMode: Text.WordWrap
        MouseArea {
            anchors.fill: parent
            onClicked: {
                clientSocket.sendTextMessage("Hello Server!");
            }
        }
    }
}



Introduced in SDK version: 6

Property Documentation

[read-only] errorString : string

The error message in case an error occurred.


host : string

The host address of the server. By default, localhost is used.


listen : bool

Set to true when the server should listen to client connections or false otherwise. When set to true, the server will listen on the specified url defined by host and port and, when accept is true, accepts incoming client connections. Otherwise the server is closed. By default, the server is not listening.


name : string

The name of this server used during the http handshake phase.


port : int

The port this server is listening on. The value must be in the range 0-65535.

By default, a port is chosen automatically.


secure : bool

Set to true to use secure (TLS) web sockets (wss) server. By default, this is false.

The secure connection uses the default Dolby Conference Phone certificate for the server. It does not verify the peer (client) certificate.


[read-only] url : url

Server url that client WebSockets can connect to. The url uses the ws:// or wss:// scheme and includes the port the server listens to and the host address of the server.


Signal Documentation

clientConnected(WebSocket webSocket)

This signal is emitted when a client connects to this server. webSocket is the newly created WebSocket client connection.