Websocket

Interfacing external applications with realvirtual.io

This interface is currently in Beta

The Websocket Realtime Interface offers high performance with low cycle times and rapid signal communication. It serves as a public interface, enabling connectivity between any application and realvirtual.io. Leveraging websocket as the underlying technology provides versatility, as it is compatible with all platforms, including WebGL.

Prerequisites

To enable the Websocket Realtime Interface, ensure that you add REALVIRTUAL_JSON to your Scripting Define Symbols. This interface utilizes Newtonsoft.Json. For detailed instructions, refer to the Newtonsoft Json section for more information.

Using the interface

You can utilize the WebSocket Realtime Interface as both a server and a client, allowing for example also to connect two Unity instances for distributed simulations.

Setting Up the Server:

  • Address: Set the address to 127.0.0.1 (localhost).

  • Port: Choose a port according to your preference. Standard ports include 80 and 443.

Configuring the Client:

  • Address: Input the server's public IP address.

  • Port: Select a port as needed.

Importing Signal Data:

During edit mode, you have the option to import signal data, simplifying the configuration process by only requiring setup on one side. To enable this feature, start the server during edit mode.

Automatic Connection:

By enabling "Connect On Play," the server and client automatically establish a connection when the simulation is initiated.

Implementing the interface as a client to your Application

To implement the Websocket Interface, the client must send the following JSON when connecting to the server:

{"tag":"init","content":"Client"}

This informs the server that the client named "Client" is connected. After the connection is established, the client will receive the server's data (which includes all PLC inputs on the server side) via a JSON like this:

{"tag":"data","content":"{"InputBools":[{"name":"Bool 2","value":true}],"InputInts":[],"InputFloats":[{"name":"Float 1","value":0.2}],"InputTexts":[],"OutputBools":[{"name":"Bool 1","value":false}],"OutputInts":[],"OutputFloats":[],"OutputTexts":[]}"}

You can utilize the following C# classes for serializing and deserializing the JSONs:

using System;
using System.Collections.Generic;

[Serializable]
public class SignalData
{
    public List<Signal<bool>> InputBools;
    public List<Signal<int>> InputInts;
    public List<Signal<float>> InputFloats;
    public List<Signal<string>> InputTexts;
    public List<Signal<bool>> OutputBools;
    public List<Signal<int>> OutputInts;
    public List<Signal<float>> OutputFloats;
    public List<Signal<string>> OutputTexts;
}

[Serializable]
public class Signal<T>
{
    public string name;
    public T value;
}

[Serializable]
public class Message
{
    public string tag;
    public string content;
}

The message structure consists of a header "tag" defining the content of the message, followed by "content". The content is always a string which can include the message or the include the full json of the serialized "SignalData". The following tags are used:

  • init: Followed by the client name sent from the client to the server at the start of the communication.

  • request_import: From the client to the server or vice versa for requesting the full input and output signal list from the server or client. This is used for signal initialization on one side based on the predefined signals on the other side.

  • data: Followed by all SignalData during signal exchange.

Last updated