Skip to Content
Internet Protocol
Unreal Engine Plugin

Websocket Client

WebSocket client for Unreal Engine: Blueprint nodes, events and C++ usage with the Internet Protocol plugin.

References

#include "websocket/wsclient.h"

How to use

Blueprint: search by GetInternetProtocolSubsystem and call CreateWebsocketClient function

C++: Use the code bellow

#include "websocket/wsclient.h"

UWSClient* client = NewObject<UWSClient>();

Variables

Handshake

  • Set/Get handshake headers.
  • To offer subprotocols, set the Sec-WebSocket-Protocol header (for example chat, json). Get Sub Protocol returns the one the server selected.
  • Since 4.0 extensions cannot be requested (Sec-WebSocket-Extensions), and a server response that selects an extension or a subprotocol that was not offered is rejected.
Handshake
Variable

Use Tls

  • New in 4.0. Encrypts the connection with TLS. With no Set Tls Options call, the secure default options are used.
  • Applies to the next connection.
Use Tls
Variable

Ping Interval Seconds

  • New in 4.0. Sends a Ping after this much silence. Default 20, 0 = never.
Ping Interval Seconds
Variable

Idle Timeout Seconds

  • New in 4.0. Closes the connection (Operation timed out) after this much time without any frame from the server. Default 60, 0 = never.
Idle Timeout Seconds
Variable

Auto Reconnect

  • New in 4.0. Connects again after an unexpected close, waiting 1, 2, 4... seconds. Never after Close or End. Default false.
  • On Close fires for the lost connection, then On Connected again once reconnected.
Auto Reconnect
Variable

Reconnect Max Delay Seconds

  • New in 4.0. Upper bound of the wait between reconnection attempts. Default 30.
Reconnect Max Delay Seconds
Variable

Functions

Is Open

  • Return true if socket is open.
Is Open
Return Value
Pure function

Local Endpoint

  • Get the local endpoint of the socket. Use this function only after open connection.
Local Endpoint
Return Value
Pure function

Remote Endpoint

  • Get the remote endpoint of the socket. Use this function only after open connection.
Remote Endpoint
Return Value
Pure function

Get Error Code

  • Return a struct of the latest error code returned by asio.
Get Error Code
Return Value
Pure function

Write

  • Sends string data on the socket.
  • It returns false if socket is closed, if buffer is empty or if the send queue limit is exceeded.
  • Fragmented messages: write with Dataframe.Fin = false to send a fragment, and finish the message with Fin = true. See Dataframe.
  • Empty messages are allowed since 4.0.
WriteTarget is WSClient
Target
Message
Dataframe
Callback
Return Value
Function
On Message SentCustom Event
Error Code
Bytes Sent
Event

Write Buffer

  • Sends buffer data on the socket.
  • It returns false if socket is closed, if buffer is empty or if the send queue limit is exceeded.
  • Fragmented messages: write with Dataframe.Fin = false to send a fragment, and finish the message with Fin = true. See Dataframe.
  • Empty messages are allowed since 4.0.
Write BufferTarget is WSClient
Target
Buffer
Dataframe
Callback
Return Value
Function
On Message SentCustom Event
Error Code
Bytes Sent
Event

Ping

  • Send a ping message.
  • It returns false if socket is closed, if buffer is empty or if the send queue limit is exceeded.
PingTarget is WSClient
Target
Callback
Return Value
Function
On Message SentCustom Event
Error Code
Bytes Sent
Event

Pong

  • Send a pong message.
  • It returns false if socket is closed, if buffer is empty or if the send queue limit is exceeded.
PongTarget is WSClient
Target
Callback
Return Value
Function
On Message SentCustom Event
Error Code
Bytes Sent
Event

Connect

  • Resolve the address and connect asynchronously. On Connected is triggered when the connection is established, On Error if it fails.
  • It returns false if the socket is already open or connecting.
ConnectTarget is WSClient
Target
Bind Opts
Return Value
Function

End

  • Perform a smooth close of socket following ws protocol (RFC 6455 closing handshake) and stop listening for data on it. 'on_close' event will be triggered.
  • Sends a Close frame with Code and Reason, then waits up to 5 seconds for the peer's Close frame before closing the TCP connection.
EndTarget is WSClient
Target
Code
Reason
Function

Close

  • Close the underlying socket immediately, without the closing handshake, and stop listening for data on it. 'on_close' event will be triggered with the given Code and Reason.
  • Prefer End for a clean disconnect; use Close to abort.
CloseTarget is WSClient
Target
Code
Reason
Function

Connect Url

  • New in 4.0. Connects to ws://host[:port]/path?query, or wss:// (TLS is chosen from the scheme). The path and query go to Handshake.Path.
Connect UrlTarget is WSClient
Target
Url
Return Value
Function

Get Sub Protocol

  • New in 4.0. Subprotocol agreed in the handshake (empty: none).
Get Sub Protocol
Return Value
Pure function

Get Round Trip Time Ms

  • New in 4.0. Round trip of the last Ping answered by a Pong, in ms (-1: none yet).
Get Round Trip Time Ms
Return Value
Pure function

Get Stats

  • New in 4.0. Traffic counters of the current connection: bytes and messages sent and received, queued bytes and the last ping round trip. See Connection Stats.
Get Stats
Return Value
Pure function

Set Tls Options

  • New in 4.0. Turns TLS on with these options. See Security Context Options.
  • The server certificate is verified with these options.
  • Applies to the next connection.
Set Tls OptionsTarget is WSClient
Target
Sec Opts
Function

Events

On Connected

  • Event triggered when a connection is estabilished.
Bind Event to On Connected
Target
Event
On ConnectedCustom Event
Handshake
Event binding

On Unexpected Handshake

  • Event triggered when the server answers the upgrade request with an invalid handshake (wrong status, missing Upgrade headers or a bad Sec-WebSocket-Accept). The connection is not opened.
Bind Event to On Unexpected Handshake
Target
Event
On Unexpected HandshakeCustom Event
Handshake
Event binding

On Message

  • Event triggered when a complete message is received. Fragmented messages are reassembled before this event fires.
  • Text messages are validated as UTF-8; messages larger than 64 MiB close the connection with code 1009.
Bind Event to On Message
Target
Event
On MessageCustom Event
Buffer
Is Binary
Event binding

On Ping

  • This event will be triggered when a ping has been received. A pong is sent back automatically, so you do not need to answer it.
Bind Event to On Ping
Target
Event
On PingCustom Event
Event binding

On Pong

  • This event will be triggered when a pong has been received.
Bind Event to On Pong
Target
Event
On PongCustom Event
Event binding

On Close

  • Event triggered when socket is closed. It fires exactly once per connection.
  • Since 4.0 close codes 1012-1014 are accepted, and codes that cannot be sent (1005, 1006, 1015, unregistered) are replaced by 1000 when you call End or Close.
  • Code is the close code received from the peer (1005 when it sent none), 1006 when the connection was lost without a Close frame, or 1002 / 1007 / 1009 when the plugin closed the connection because of a protocol violation, invalid UTF-8 or a message that was too large.
Bind Event to On Close
Target
Event
On CloseCustom Event
Code
Reason
Event binding

On Error

  • Event triggered if any error occur during async process.
Bind Event to On Error
Target
Event
On ErrorCustom Event
Error Code
Event binding
Edit on GitHub

On this page