# HoloPlayCore.Message

A class to represent messages being sent over to HoloPlay Service. This is the base class, and users should aim to use the child classes that we provide below if possible unless you need a very specific custom Message. If you use the base class, you will have to construct the objects yourself.

All Message classes have `cmd`, and only quilt related ones have `bin` for binary data of the quilt.

See here for a list of [error codes](https://lfdocs.lookingglassfactory.com/legacy/legacy-software/corejs/hops-related#error-codes).

### **Constructor**

* `cmd` object of the corresponsding settings for each command type.
* `bin` binary data of the quilt, if it is a quilt-related message.

**Examples**

```javascript
let msg = new Message({"info": ""}, binaryData);
client.sendMessage(msg)
  .then(() => console.log("Response received"))
  .catch((err) => console.log(err))
```

### **Methods**

.toCbor()

Convert the class instance into CBOR format.

## InitMessage

**Extends Message**

Extends the base Message class. If you provide following parameters in the constructor of `HoloPlayCore.Client`, this command will be called automatically, and you don't need to call it yourself.

**Parameters**

* `appId` **string** optional; an identifier to specify what application it is in HoloPlay Service. If the same appId is provided, different connections will be treat as the same application and have access to cached quilts. If not provided, HoloPlay Service will generate a random string.
* `isGreedy` **boolean** optional; this feature is currently not implemented.
* `oncloseBehavior` **string** optional, determines behavior when all instances of a given app ID disconnect. Can be `wipe`, `hide`, or `none`, default is `hide`.&#x20;
  * `wipe` : clear the screen, display the default background.
  * `hide` : hide the window on the Looking Glass.
  * `none` : do nothing.
* `debug` **boolean** optional; default is `false`. If true, there will be logging messages in console.&#x20;

Returns information of the connected Looking Glass devices, in the format of [device status response](https://lfdocs.lookingglassfactory.com/legacy/legacy-software/corejs/hops-related#device-status).

**Examples**

```javascript
let initMsg = new InitMessage("Test Looking Glass app", false, "wipe");
client.sendMessage(initMsg)
 .then((res) => console.log(res));
```

## InfoMessage

**Extends Message**

Extends the base Message class. Gets info from HoloPlay Service.

Returns information of the connected Looking Glass devices, in the format of [device status response](https://lfdocs.lookingglassfactory.com/legacy/legacy-software/corejs/hops-related#device-status).

**Examples**

```javascript
let infoMsg = new InfoMessage();
client.sendMessage(infoMsg)
 .then((res) => console.log(res));
```

## ShowMessage

**Extends Message**

Extends the base Message class. Show a quilt directly without caching it.

**Parameters**

* `settings` **object** optional, default `{vx: 1,vy: 1,aspect:1.6}`, for detailed format see [quilt settings](https://lfdocs.lookingglassfactory.com/keyconcepts/quilts).
* `bindata` **Uint8Array**, optional, default `''`, but you will have to set it later.
* `targetDisplay` **int**, optional, default `0`.

**Examples**

```javascript
let xhttp = new XMLHttpRequest();
xhttp.responseType = 'arraybuffer';
xhttp.onreadystatechange = function () {
    if (this.readyState == 4) {
        if (this.status == 200) {
            const quiltData = new Uint8Array(this.response);
            const showCmd = new ShowMessage(
                {vx: 5, vy: 9, vtotal: 45, aspect: 1.6},
                quiltData,
                0
            );
            client.sendMessage(showCmd)
            .then(() => alert("Quilt displayed!"));
        } else {
            console.log("Could not load " + quiltUrl + "!");
        }
    }
};
xhttp.open("GET", quiltUrl, true);
xhttp.send();
```

## CacheMessage

**Extends Message**

Extends the base Message class. This message sends a quilt image to the cache, and you can send a `ShowCachedMessage` later to show it.

**Parameters**

* `name` **string**, the name of the quilt being cached, for later retrieval.
* `settings` **object** optional, default `{vx:1,vy:1,aspect:1.6}`, for detailed format see [quilt settings](https://lfdocs.lookingglassfactory.com/keyconcepts/quilts).
* `bindata` (optional, default `''`).
* `show` (optional, default `false`).

## ShowCachedMessage

**Extends Message**

Extends the base Message class. Retrieve and show a previously cached quilt by name. If you're not sure if a quilt is cached, use the `CheckMessage` command to check. If you try to show an uncached image, HoloPlay Service will respond with error code 3.

**Parameters**

* `name` **string**, the name of the quilt in the cache.
* `targetDisplay` **int**, optional, default `0`.
* `settings` **object** optional, default `{vx:5,vy:9,aspect:1.6}`, for detailed format see [quilt settings](https://lfdocs.lookingglassfactory.com/keyconcepts/quilts). (THERE'S STILL INCONSISTENCY IN WHAT THE DEFAULT SETTINGS ARE, NEEDS TO BE RESOLVED)

## CheckMessage

**Extends Message**

Check if a quilt exist in the cache. Extends the base Message class.

**Parameters**

* `name` **string** name of the quilt (optional, default `''`).

**Example**

```javascript
let checkCmd = new CheckMessage('quilt1')
client.sendMessage(checkCmd)
  .then((res) => {
    if (res['error'] === 0) {
        // quilt found in cache
        let showCachedCmd = new ShowCachedMessage('quilt1');
        client.sendMessage(showCachedCmd);
    } else if (res['error'] === 5) {
        // quilt not cached
        let showCmd = new ShowMessage({vx:5, vy:9, aspect: 1.6}, quiltBinaryData);
        client.sendMessage(showCmd);
    }
  });
```

## DeleteMessage

**Extends Message**

Extends the base Message class. Deletes a previously cached quilt from HoloPlay Service.

**Parameters**

* `name` **string**;  name of the quilt.

## WipeMessage

**Extends Message**

Extends the base Message class. Wipes the image in Looking Glass and displays the background image.

**Parameters**

* `targetDisplay` (optional, default `0`).
