VibHub-Browser: Difference between revisions

From VibHub Wiki
Jump to navigation Jump to search
Jasx (talk | contribs)
No edit summary
Jasx (talk | contribs)
No edit summary
Line 153: Line 153:
|Internal handler for device battery updates. Data is an object with {string id, bool batteryLow, int mv(millivolts), int xv(max millivolts)}
|Internal handler for device battery updates. Data is an object with {string id, bool batteryLow, int mv(millivolts), int xv(max millivolts)}
|-
|-
|addDevice
|m,o
|string deviceID
|VhDevice device
|Yes
|Requires an active connection. Tells the server that you want permissions to manage this device by id. Permissions are NOT lost if the device disconnects and reconnects. Permissions ARE lost if your app disconnects.
|-
|remDevice
|m,o
|VhDevice device
|
|
|Yes
|Requires an active connection. Tells the server that you no longer want to manage a device. Note that devices are automatically disowned when you disconnect the app. But it's good practice to call this if you want to swap devices.
|-
|wipeDevices
|m,o
|
|
|
|
|Yes
|Removes all devices from your app.
|-
|getDevice
|m,o
|string deviceID
|VhDevice device OR undefined
|
|
|Gets a maintained device by ID if it exists, otherwise returns false.
|-
|sendPWM
|m,o
|VhDevice device
|
|
|
|
|Updates the vibration value of a device. Use VhDevice set (listed below) instead, as it automatically prevents overloading the device with too many requests.
|-
|sendSingleChannelPWM
|m,o
|VhDevice device, int channel...
|
|
|Same as sendPWM but for specific ports. Use VhDevice set instead.
|-
|sendProgram
|m,o
|VhDevice device, VhProgram program
|
|
|Sends a program. Use VhDevice sendProgram instead.
|-
|getBattery
|m
|VhDevice device
|
|
|Requests a battery update. Use VhDevice getBattery instead. If successful, the onDeviceBattery event will be raised.
|-
|sendCustomMessage
|m
|VhDevice device, var data
|
|
|Emits a dCustom event (advanced).
|-
|tick
|m,o
|
|
|
|Used internally to rate-limit updates and prevent overloading your device with requests.
|-
|_addScript
|m,o
|
|
|
|Used internally to make sure socket.io exists in the browser.
|}
|}

Revision as of 12:09, 29 January 2026

The library can be cloned from here.

Setup

Start by importing the library:

import {default as VH, VhProgram as Program, VhStage as Stage} from 'vh-socket.js';

Next you'll want to extend VH with a class of your own.

class App extends VH{

Constructor:

constructor(...args){
	super(...args);
}

All done for our VibHub manager class! Let's set all ports on our device to max!

(async () => { const manager = new App(); manager.onDeviceOnline = function(device){ device.set(255); // Sets all ports on the device to max intensity (or 1/4 if you enable high res) }; await manager.addDevice("<your device ID>"); })();

VhSocket

Versions:

  • m - 2026 VibHub Micro
  • o - Original VibHub 4x AA battery version.

Overridable

These methods are meant to be overridden by your extended class or on the fly.

Method Versions Args Async Description
onDeviceOnline m,o VhDevice device Raised when a device has come online.
onDeviceOffline m,o VhDevice device Raised when a device has gone offline.
onDeviceBattery m VhDevice device Battery status has been updated
onCustomMessage m string device_ID, string socket_ID, var data Raised when a custom message has been received (advanced).
onConnected m,o Raised when the app itself has connected to a VibHub server.
onDisconnected m,o Raised when the app itself has disconnected from the VibHub server.
onTick m,o Raised n times per second (based on what you passed to constructor).

Methods

Method Versions Args Returns Async Description
constructor m,o string appName, string server = "https://vibhub.io", int port = 443, int fps = 30, bool autoHighRes VhSocket Library constructor.

- appName can be anything you'd like to call your app (mainly used for debugging. - server is the server to connect to. - port is the port (hurdur). Usually 443 (SSL) or 80 - fps sets a max limit on how often we can send messages to the device. I don't recommend setting this to more than 60. - autoHighRes tries to automatically enable High Res mode if the device supports it. High Res gives you a resolution of 1024 instead of 256, for more granular control.

begin m,o VhSocket Yes Starts the app, connecting via socket.io and binding events.
isConnected m,o bool Returns true if we're connected to the server.
setName m,o bool Yes Sends the app name to the server (auto handled in begin). Returns true on success.
handleNewIndex m,o array devices Yes Internal handler for the server refreshing our managed devices.
handleDeviceOnline m,o array data Internal handler for device data received. Data is an array of: string id, string socketID, var metadata
handleDeviceOffline m,o data Internal handler for device disconnect. Data is an array of: string id
handleDeviceBattery m data Internal handler for device battery updates. Data is an object with {string id, bool batteryLow, int mv(millivolts), int xv(max millivolts)}
addDevice m,o string deviceID VhDevice device Yes Requires an active connection. Tells the server that you want permissions to manage this device by id. Permissions are NOT lost if the device disconnects and reconnects. Permissions ARE lost if your app disconnects.
remDevice m,o VhDevice device Yes Requires an active connection. Tells the server that you no longer want to manage a device. Note that devices are automatically disowned when you disconnect the app. But it's good practice to call this if you want to swap devices.
wipeDevices m,o Yes Removes all devices from your app.
getDevice m,o string deviceID VhDevice device OR undefined Gets a maintained device by ID if it exists, otherwise returns false.
sendPWM m,o VhDevice device Updates the vibration value of a device. Use VhDevice set (listed below) instead, as it automatically prevents overloading the device with too many requests.
sendSingleChannelPWM m,o VhDevice device, int channel... Same as sendPWM but for specific ports. Use VhDevice set instead.
sendProgram m,o VhDevice device, VhProgram program Sends a program. Use VhDevice sendProgram instead.
getBattery m VhDevice device Requests a battery update. Use VhDevice getBattery instead. If successful, the onDeviceBattery event will be raised.
sendCustomMessage m VhDevice device, var data Emits a dCustom event (advanced).
tick m,o Used internally to rate-limit updates and prevent overloading your device with requests.
_addScript m,o Used internally to make sure socket.io exists in the browser.