WebSocket Events Documentation
FoxWorn3365
WebSocketEvents

WebSocketEvents


@dir:/connectivity;

WebSocket Events Lib - Connectivity
As we fully support Open Source and the ability of those using the plugin to create their own library,
so this documentation will help you move through our protocols to establish a secure connection via WebSocket.

Complex and Simple connections
Our WebSockets distinguish between two different types of connectivity, depending on what the library you use offers:

Complex connection
Complex connections are the connections usually initialized by the WebSocket class of JavaScript and feature complex header sending and message encoding.
It is the default type, so it should not be defined in the headers.

Simple connection
Simple connections, on the other hand, represent the sending and receiving of a simple message that is not complex (but of variable length)
and skips the process of "decoding" and "encoding" the messages themselves.
To use this type, it is necessary to define it in the headers:
Type: simple-connection

Connection headers
It is preferable, even for the purpose of authentication, to send headers to connect to the WebSocket server:
GET / HTTP/1.1
Type: simple-connection
Host: {$this->host}
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Key: [random generated key]
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Authorization: Basic [base64 of the token]

Authentication
Authentication is a key step in connecting to the WebSocket: the latter will calculate permissions based on the configuration of the plugin.
There are three different types of authentication that are equally important, although the third can be disabled from the configuration.

1 | Basic URL-Auth
Available only in WebSocket version 13 allows the token NOT IN base64 to be passed directly into the connection URI:
wss://mybeautifultoken@server.mypmmpserver.uwu

2 | Headers Auth
Always available, it is simply a matter of including in the headers also one dedicated to authentication with the IN base64 token:
Authorization: Basic 

3 | DoubleRequest-Auth
This authentication mode is the most complicated and sensitive, so it can be disabled from the plugin settings.
Here is an explanation of how the connection should be made:



Heartbeat
Various functions can be performed to keep the connection alive, simply by sending a message:

Simple Heartbeat
An ordinary heartbeat, can be triggered by sending one of the following messages:
1) "hello world"
2) "heartbeat"
There will be a simple plain-text negligible answer

Heartbeat + Server Chunk
While keeping the connection alive you can as well receive the information about the server (in a JSON) using the following message:
heartbeat+hello world+server_chunk
The entire server object (to be imported into the respective class) will be received


Fetch a Player
Being able to retrieve real-time information about a player is one of the most useful and basic functions of this system.
Though the player is passed directly with each event where one of the subjects it is possible to retrieve externally to the events all the complete information about a player by sending the following JSON request to the WebSocket Server:
{
    "action":"GET",
    "fetch":"player",
    "target":"[USERNAME]",
    "cached":false
}
Note: You must set the cached arg to false!

The response will be a simple JSON with "status" (200 if successful) and the player information in "data"

WARNING
In case the permissions for the token used by the client regarding players is not on `*` then only the permitted values (given in the plugin config) will be included in the response!

WARNING
To avoid any possible error, the server will wait exactly 1 second before sending the response


Fetch the Server
It is possible to retrieve information about the server using the following JSON:
{
    "action":"GET",
    "fetch":"server",
    "target":null,
    "cached":null
}

The response will be a simple JSON with "status" (200 if successful) and the server informations in "data"

WARNING
In case the permissions for the token used by the client regarding server is not on `*` then only the permitted values (given in the plugin config) will be included in the response!


Execute an action
Another important element of the plugin is the ability to execute simple direct commands to the player or console.

Player
Performing actions for a player is easy.
In fact, it will be enough to provide the player's username and, of course, the function to be performed.
{
    "action":"EXECUTE",
    "fetch":"player",
    "target":"[USERNAME]",
    "action":"[ACTION]",
    "args" => ["[ARG 1]", "[ARG 2]"]
}
Note: All executable functions are in effect the function names of the Player class!

Server
Performing actions for the console is easy.
In fact, it will be enough to provide the command to be performed.
{
    "action":"EXECUTE",
    "fetch":"server",
    "target":null
    "action":"[ACTION]",
    "args" => ["[ARG 1]", "[ARG 2]"]
}


Receive Events
Events are the main part of this plugin and therefore are both easy to manage and protected by the plugin configuration.
In fact, for each token it is possible to define what events are transmitted.

Base structure
It's a simple JSON:
(This example is for the player_join event)
{
  "status": 201,
  "type": "event",
  "event": true,
  "data": {
    "event": true,
    "eventName": "player_join",
    "eventClass": "playerJoin",
    "data": {
      "player": {
        "type": "player",
        "online": true,
        "display_name": "FoxWorn3365",
        "gamemode": "Survival",
        "healt": 14,
        "id": 1,
        "last_played": 1684079403559,
        "location": {
          "yaw": 295.1971130371094,
          "pitch": 0,
          "x": 324.9117,
          "y": 65,
          "z": 308.4709
        },
        "position": {
          "x": 324.9117,
          "y": 65,
          "z": 308.4709
        },
        "max_healt": 20,
        "name": "FoxWorn3365",
        "name_tag": "FoxWorn3365",
        "spawn": {
          "world": {
            "updateEntities": {
              "1": {
                "spawned": true,
                "deadTicks": 0,
                "boundingBox": {
                  "minX": 324.6117,
                  "minY": 65,
                  "minZ": 308.17089999999996,
                  "maxX": 325.2117,
                  "maxY": 66.8,
                  "maxZ": 308.7709
                },
                "onGround": true,
                "size": {},
                "keepMovement": true,
                "fallDistance": 0,
                "ticksLived": 0,
                "lastUpdate": 1452,
                "canCollide": true,
                "isCollided": true,
                "isCollidedHorizontally": false,
                "isCollidedVertically": false,
                "noDamageTicks": 0
              }
            },
            "stopTime": false,
            "timings": {
              "setBlock": {},
              "doBlockLightUpdates": {},
              "doBlockSkyLightUpdates": {},
              "doChunkUnload": {},
              "scheduledBlockUpdates": {},
              "randomChunkUpdates": {},
              "randomChunkUpdatesChunkSelection": {},
              "doChunkGC": {},
              "entityTick": {},
              "doTick": {},
              "syncChunkSend": {},
              "syncChunkSendPrepare": {},
              "syncChunkLoad": {},
              "syncChunkLoadData": {},
              "syncChunkLoadFixInvalidBlocks": {},
              "syncChunkLoadEntities": {},
              "syncChunkLoadTileEntities": {},
              "syncChunkSave": {}
            },
            "tickRateTime": 1.5540122985839844
          },
          "x": 256,
          "y": 70,
          "z": 256
        },
        "uuid": "c96c3895-167e-33a3-8e92-a499e0f2dbd0",
        "viewers": [],
        "world": "world",
        "skin": {
          "cape": {
            "data": ""
          },
          "data": null,
          "id": "Custom6893d58b-3bda-3e67-9842-02bd1dda2fa7"
        }
      }
    }
  }
}