MultiShock WebSocket API

Obtaining Port

The default port for the WebSocket server is 8765. If the default port is unavailable MultiShock will try the ports 8766 - 8769 inclusive for any availability.

Obtaining Port (Alternative)

An alternative way to obtain to determine which port MultiShock is using is to use zeroconf or another service discovery of your choice.

The service you are looking for is MultiShock._ws._tcp.local.

Below is an example of how to get the WebSocket server IP and port using Python.

import socket
from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange, ServiceInfo

class MyServiceListener:
    def __init__(self):
        self.info = None

    def add_service(self, zeroconf, type, name):
        self.info = zeroconf.get_service_info(type, name)
        if self.info:
            print(f"Service {name} added")
            print(f"Address: {socket.inet_ntoa(self.info.addresses[0])}")
            print(f"Port: {self.info.port}")

    def remove_service(self, zeroconf, type, name):
        print(f"Service {name} removed")

def discover_service(service_type="_ws._tcp.local."):
    zeroconf = Zeroconf()
    listener = MyServiceListener()
    browser = ServiceBrowser(zeroconf, service_type, listener)
    
    try:
        # Wait for a while to discover the service
        import time
        time.sleep(5)
    finally:
        zeroconf.close()
    
    return listener.info

if __name__ == "__main__":
    # Discover the service
    service_info = discover_service("_ws._tcp.local.")
    
    if service_info:
        address = socket.inet_ntoa(service_info.addresses[0])
        port = service_info.port
        print(f"Discovered service at {address}:{port}")
    else:
        print("No service found")

Sending Commands

Warning

All commands need to include the auth_key within the WebSocket request body. This variable is user configurable in MultiShockSettingsWebsocket Auth Key.

The Auth Key can be changed at any time via MultiShock.

It is important that the Auth Key you specify is updated when you make a request.

All commands should be sent to ws://localhost:port

Requesting Devices/Shockers

Make a WebSocket request to the server on the port you discovered under Obtaining Port, and send the following payload as JSON.

{
	"cmd": "get_devices",
	"auth_key": "keyhere"
}

You can expect the response to resemble the below example.

[
  {
    "name": "PiShock 3",
    "id": 12, //PiShock ClientID
    "shockers": [
      {
        "name": "Shocker 1",
        "identifier": 6345 //Shocker ID
      },
      {
        "name": "Shocker 2",
        "identifier": 8346 //Shocker ID
      }
    ]
  },
  {
    "name": "Pishock 2",
    "id": 34, //PiShock ClientID
    "shockers": [
      {
        "name": "Shocker 3",
        "identifier": 1042 //Shocker ID
      },
      {
        "name": "Shocker 4",
        "identifier": 1024 //Shocker ID
      },
    ]
  }
]

These ids can be used for operating Shockers as described further in the next section.

Operating Shockers

If you would like to send an operate request, send the following payload to the the WebSocket server.

{
  "cmd": "operate",
  "value": {
    "intensity": 10, // 1 - 100 - int
    "duration": 1, // 0.1 - 15 - float
    "shocker_option": "all", // all, random
    "action": "shock", // shock, vibrate, beep, end
    "shocker_ids": [], // [] - List of shocker ids
    "device_ids": [], // [] - list of pishock client ids, if one of these is provided it will activate all shockers associated with it
    "warning": false, // true, false - will send a vibrate with the same intensity and duration
    "held": false, // true, false - for continuous commands
  },
  "auth_key": "keyhere"
}
Specify ids

Ensure you specify the shocker_ids and/or any device_ids for the Shockers you wish to operate.

Please note that all shocker_ids and device_ids are integers.

Ensure you replace "keyhere" with the Auth Key specified in MultiShock.