Send scripts to a device player
Your app plays media with funscripts (a game, a video site wrapper, an editor) and you want devices to follow it, without writing device code. The library’s OFS server does it in one call: it serves the OpenFunscripter WebSocket API, speaks ofs-ws-extended and announces itself over Funlink. RustyFunPlayer shows a Connect card, follows your playback and drives every device the user has, on every axis, with the user’s limits.
1. Start the server
import { serveOfs } from "funlink";
const ofs = await serveOfs({
id: "my-game", // stable per installation
name: "My Game",
version: "1.0.0",
onEvent: (event) => {
if (event.type === "devices") showDevices(event.state.outputs);
if (event.type === "command") handleCommand(event.name, event.data); // change_play, change_time, …
},
});
// On exit:
await ofs.close();
#![allow(unused)]
fn main() {
use funlink::ofs::{OfsEvent, OfsServer};
let ofs = OfsServer::builder("My Game")
.id("my-game") // stable per installation
.version("1.0.0")
.start(|event| match event {
OfsEvent::Devices { state, .. } => show_devices(&state.outputs),
OfsEvent::Command { name, data, .. } => handle_command(&name, &data),
_ => {}
})?;
// Dropping `ofs` stops it.
}
using Funlink;
await using var ofs = await OfsServer.StartAsync(
new OfsServerOptions("My Game") { Id = "my-game", Version = "1.0.0" },
e =>
{
switch (e)
{
case OfsDevicesChanged d: ShowDevices(d.State.Outputs); break;
case OfsCommand c: HandleCommand(c.Name, c.Data); break;
}
});
import io.github.faplandpartydev.funlink.*;
try (var ofs = Ofs.serve(OfsOptions.of("My Game").withId("my-game").withVersion("1.0.0"), event -> {
if (event instanceof OfsEvent.Devices d) showDevices(d.state().outputs());
if (event instanceof OfsEvent.Command c) handleCommand(c.name(), c.data());
})) {
// Run your app.
}
The OFS server is not part of the C library: microcontrollers are the devices, not the player.
The OFS server is not part of the Godot add-on. A Godot .NET game can use the C# library’s OfsServer; a GDScript game can serve the OFS WebSocket API itself with WebSocketPeer and announce it with the add-on.
| Option | Default | Meaning |
|---|---|---|
id | required | Your app’s fixed Funlink id, such as my-game. It also picks the port. |
name, version | – | Shown on the player’s card and sent in the handshake. |
path | /ofs | WebSocket path. |
announce | on | Announce over Funlink. Turn off to only serve. |
extended | on | Speak ofs-ws-extended. Off serves plain OFS only. |
The server listens on 127.0.0.1 only and refuses web pages and, on Linux, other users.
Port
You do not choose a port: it is derived from the id, so your app gets the same port on every machine and other apps rarely collide. my-game always serves on 21976. If that port is busy, the next free one is used. ofsPort(id) (default_port, DefaultPortFor, defaultPort) tells you the number up front, for example to show it in your settings. The rule is part of the ofs-ws-extended specification.
Players find you through Funlink, so users never type the port. Only if a user must pin one, pass manualPort (manual_port, ManualPort, withManualPort); it is used exactly and fails if taken.
2. Tell it what plays
Call the setters whenever something changes. The server keeps the latest state and replays it to every player that connects later.
ofs.setMedia("/videos/round.mp4"); // null when nothing plays
ofs.setDuration(60);
ofs.setScript("round.funscript", script); // the parsed funscript object
ofs.setScript("round.twist.funscript", twist); // more axes: name them like files
ofs.setPlaying(true);
ofs.setTime(video.currentTime); // as often as you like
ofs.setSpeed(1);
ofs.removeScript("round.twist.funscript");
ofs.clearScripts();
#![allow(unused)]
fn main() {
ofs.set_media(Some("/videos/round.mp4")); // None when nothing plays
ofs.set_duration(60.0)?;
ofs.set_script("round.funscript", script)?; // serde_json::Value
ofs.set_script("round.twist.funscript", twist)?;
ofs.set_playing(true);
ofs.set_time(position)?;
ofs.set_speed(1.0)?;
ofs.remove_script("round.twist.funscript");
ofs.clear_scripts();
}
ofs.SetMedia("/videos/round.mp4"); // null when nothing plays
ofs.SetDuration(60);
ofs.SetScript("round.funscript", script); // JsonElement
ofs.SetScript("round.twist.funscript", twist);
ofs.SetPlaying(true);
ofs.SetTime(position);
ofs.SetSpeed(1);
ofs.RemoveScript("round.twist.funscript");
ofs.ClearScripts();
ofs.setMedia("/videos/round.mp4"); // null when nothing plays
ofs.setDuration(60);
ofs.setScript("round.funscript", script); // Map/List as parsed JSON
ofs.setScript("round.twist.funscript", twist);
ofs.setPlaying(true);
ofs.setTime(position);
ofs.setSpeed(1);
ofs.removeScript("round.twist.funscript");
ofs.clearScripts();
The OFS server is not part of the C library: microcontrollers are the devices, not the player.
The OFS server is not part of the Godot add-on. A Godot .NET game can use the C# library’s OfsServer; a GDScript game can serve the OFS WebSocket API itself with WebSocketPeer and announce it with the add-on.
Name multi-axis scripts like files: round.funscript (stroke), round.twist.funscript, round.roll.funscript, … The player maps them to its axes.
3. Show and adjust devices
Players that speak ofs-ws-extended report their devices: the devices event carries each output’s kind, status, update rate and per-axis travel range, plus what plays. players lists the connected players with their latest state.
Move a device’s travel range, for example from your own settings screen:
ofs.setRange(undefined, "serial:0", "L0", 0.1, 0.9); // undefined = every extended player
#![allow(unused)]
fn main() {
ofs.set_range(None, "serial:0", "L0", 0.1, 0.9)?; // None = every extended player
}
ofs.SetRange(null, "serial:0", "L0", 0.1, 0.9); // null = every extended player
ofs.setRange("serial:0", "L0", 0.1, 0.9); // or setRange(playerId, …)
The OFS server is not part of the C library: microcontrollers are the devices, not the player.
The OFS server is not part of the Godot add-on. A Godot .NET game can use the C# library’s OfsServer; a GDScript game can serve the OFS WebSocket API itself with WebSocketPeer and announce it with the add-on.
The player applies the change like its own slider and reports it in the next devices event. Players without the extension simply get plain OFS.
4. Live scripts (generated on the fly)
Scripts don’t have to be files. For motion your app creates while it runs, for example from game state, open a live script and push actions as you go. Players that support funscript_append, such as RustyFunPlayer, extend the script in place without restarting the motion; others get the whole script again, a few times a second.
const live = ofs.liveScript("game.funscript");
ofs.setPlaying(true);
setInterval(() => {
const now = clock.ms(); // your media time in ms
ofs.setTime(now / 1000);
live.push(now + 200, intensity()); // ~200 ms ahead, pos 0–100
}, 100);
// Later:
live.remove();
#![allow(unused)]
fn main() {
let live = ofs.live_script("game.funscript")?;
ofs.set_playing(true);
loop {
let now = clock_ms();
ofs.set_time(now as f64 / 1000.0)?;
live.push(now + 200, intensity())?; // ~200 ms ahead, pos 0–100
std::thread::sleep(std::time::Duration::from_millis(100));
}
}
using var live = ofs.CreateLiveScript("game.funscript");
ofs.SetPlaying(true);
while (running)
{
var now = clock.Milliseconds;
ofs.SetTime(now / 1000.0);
live.Push(now + 200, Intensity()); // ~200 ms ahead, pos 0–100
await Task.Delay(100);
}
try (var live = ofs.liveScript("game.funscript")) {
ofs.setPlaying(true);
while (running) {
long now = clock.millis();
ofs.setTime(now / 1000.0);
live.push(now + 200, intensity()); // ~200 ms ahead, pos 0–100
Thread.sleep(100);
}
}
The OFS server is not part of the C library: microcontrollers are the devices, not the player.
The OFS server is not part of the Godot add-on. A Godot .NET game can use the C# library’s OfsServer; a GDScript game can serve the OFS WebSocket API itself with WebSocketPeer and announce it with the add-on.
atis media time in milliseconds and must increase with every push;posis 0–100.- Push a little ahead of
setTime(100–250 ms) so the player can move smoothly towards the next point. - Use one live script per axis:
game.funscript,game.twist.funscript, … - Old actions are trimmed after
maxActions(8000 by default, which keeps each message under the 256 KiB limit).clear()empties the script,remove()ends it. - Players that connect later get the script so far, then the new actions.
Events
| Event | When |
|---|---|
connected | A player opened the socket; it has already received the current state. |
hello | The player speaks ofs-ws-extended; its name and version are known. |
devices | A device report arrived (state.outputs, state.axes, state.playback). |
command | The player asks you to change_play, change_time or change_playbackspeed. Apply it if you want; the server does not. |
disconnected | The player left. |
Without the library
The protocol is small enough to serve yourself. The rest of this page shows what the OFS server does on the wire.
Serve the OFS WebSocket API
Listen on 127.0.0.1 (for example ws://127.0.0.1:8080/ofs). Each message is one JSON text frame. Send these events whenever something changes:
| Event | data |
|---|---|
media_change | { "path": "/videos/round.mp4" }, or { "path": null } when nothing plays |
funscript_change | { "name": "round.funscript", "funscript": { ... } }, the whole script |
funscript_remove | { "name": "round.funscript" } |
project_change | {}: forget all scripts |
play_change | { "playing": true } |
time_change | { "time": 12.3 } in seconds |
duration_change | { "duration": 60.0 } |
playbackspeed_change | { "speed": 1.0 } |
Name multi-axis scripts like files: round.funscript (stroke), round.twist.funscript, round.roll.funscript, … The player maps them to its axes.
A minimal server with Node.js and ws:
import { WebSocketServer } from "ws";
const server = new WebSocketServer({ host: "127.0.0.1", port: 8080, path: "/ofs" });
const send = (name: string, data: unknown) =>
server.clients.forEach((c) => c.send(JSON.stringify({ type: "event", name, data })));
server.on("connection", (socket) => {
// Bring a new player up to date.
socket.send(JSON.stringify({ type: "event", name: "media_change", data: { path: "/videos/round.mp4" } }));
socket.send(JSON.stringify({ type: "event", name: "funscript_change",
data: { name: "round.funscript", funscript: script } }));
});
// From your player:
video.on("play", () => send("play_change", { playing: true }));
video.on("pause", () => send("play_change", { playing: false }));
video.on("timeupdate", () => send("time_change", { time: video.currentTime }));
The player may send change_play, change_time and change_playbackspeed commands with the same shapes; you may ignore them.
Announce it
import { announce } from "funlink";
const funlink = announce({
id: installId,
name: "My Game",
services: [{ protocol: "ofs-ws", endpoint: "ws://127.0.0.1:8080/ofs" }],
});
The user clicks Connect on the card and playback starts following your app.
Show and adjust devices
With ofs-ws-extended the player also reports its devices on the same socket, and your app can change their travel ranges, for example to show “Handy connected, stroke 10–90 %” in your settings.
-
Announce the same endpoint twice, extended first:
services: [ { protocol: "ofs-ws-extended", endpoint: "ws://127.0.0.1:8080/ofs" }, { protocol: "ofs-ws", endpoint: "ws://127.0.0.1:8080/ofs" }, ], -
Answer the player’s hello:
socket.on("message", (raw) => { const message = JSON.parse(String(raw)); if (message.type === "command" && message.name === "device_hello") { socket.send(JSON.stringify({ type: "event", name: "device_hello", data: { protocol: 1, application: "My Game", version: "1.0.0" } })); } if (message.type === "command" && message.name === "device_state") { showDevices(message.data.outputs); // status, rate and axis ranges per output } }); -
Move a range:
socket.send(JSON.stringify({ type: "event", name: "device_set_range", data: { output: "serial:0", axis: "L0", minimum: 0.1, maximum: 0.9 } }));
Players that do not know the extension keep the plain OFS behavior, so step 3 never breaks anything. Field details are in the ofs-ws-extended reference.