FB_IotWebSocketClient

FB_IotWebSocketClient 1:

This function block enables communication with a WebSocket server. A client can manage a connection to exactly one WebSocket server. The Execute() method must be called cyclically as background communication to the server.

All input parameters are only processed when a connection is established. All parameters of type STRING expect the UTF-8 format. This corresponds to the STRING format for the 7-bit ASCII characters.

Syntax

FUNCTION BLOCK FB_IotWebSocketClient
VAR_INPUT
    sHostName              : STRING;
    nHostPort              : UINT;
    sUri                   : STRING;
    sProtocol              : STRING;
    sOrigin                : STRING;
    stTLS                  : ST_IotSocketTls;
    bPerMessageDeflate     : BOOL;
    bKeepAlive             : BOOL;
    nKeepAliveInterval     : UINT;
    nKeepAliveCloseTimeout : UINT;
    nConnectResponseTimeout: UINT;
    nMaxRecvFrameSize      : UDINT;
    nMaxRecvMsgSize        : UDINT;
    nMaxSendFrameSize      : UDINT;
END_VAR
VAR_OUTPUT
    bError                 : BOOL;
    hrErrorCode            : HRESULT;
    eConnectionState       : ETcIotWebSocketStatus;
    bConnected             : BOOL;
    nCloseReason           : UINT;
END_VAR

FB_IotWebSocketClient 2: Inputs

Name

Type

Description

sHostName

STRING

sHostName can be specified as name or as IP address. If no information is provided, the local host is used.

nHostPort

UINT

The host port is specified here. The default is 80.

sUri

STRING

Optional parameter to specify a URI for the WebSocket opening handshake.

sProtocol

STRING

Optional parameter to specify a sub-protocol for the WebSocket opening handshake.

sOrigin

STRING

Optional parameter to specify an origin for the WebSocket opening handshake.

stTLS

ST_IotSocketTls

If the server offers a TLS-secured connection, the required configuration can be implemented here.

bPerMessageDeflate

BOOL

Enables the permessage deflate compression extension.

bKeepAlive

BOOL

Enables keep-alive ping messages sent by the client. The default setting is TRUE.

nKeepAliveInterval

UINT

Specifies how many seconds after no messages have been received from the server the client should send a ping message.

nKeepAliveCloseTimeout

UINT

Specifies how many seconds the client should wait for the ping response.

nConnectResponseTimeout

UINT

Specifies how many seconds the client should wait for the server's handshake response.

nMaxRecvFrameSize

UDINT

Maximum receivable frame size. Standard size is 16#10000.

nMaxRecvMsgSize

UDINT

Maximum receivable message size. Standard size is 16#100000.

nMaxSendFrameSize

UDINT

Maximum frame size that can be sent. Standard size is 16#4000.

FB_IotWebSocketClient 3: Outputs

Name

Type

Description

bError

BOOL

Becomes TRUE when an error situation occurs.

hrErrorCode

HRESULT

Returns an ADS return code. An explanation of the possible ADS return codes can be found in the Appendix.

eConnectionState

ETcIotWebSocketStatus

Specifies the state of the connection from the client to the server as the enumeration ETcIotWebSocketState.

bConnected

BOOL

TRUE if the connection to the host is established and the WebSocket handshake was successful.

nCloseReason

UINT

WebSocket status code as defined in RFC 6455.

FB_IotWebSocketClient 4: Methods

Name

Description

CheckProtocol

This method can optionally be overridden to check the protocol returned by the server when the connection is established.

Connect

Method for establishing the connection to the WebSocket server.

Disconnect

Method for disconnecting the connection to the WebSocket server.

Execute

Must be called cyclically as background communication to the WebSocket server. If messages have been received, the Callback method is triggered once for each message.

OnWebSocketClose

Callback method that is triggered when the WebSocket connection is closed.

OnWebSocketMessage

Callback method that is triggered when a message is received.

SendMessage

Method for sending a message.

Requirements

Development environment

Target platform

PLC libraries to include

TwinCAT v3.1.4026.x

IPC or CX (x86, x64, ARM)

Tc3_IotBase (3.5.1 or higher)