snapcast-control

Crates.iosnapcast-control
lib.rssnapcast-control
version0.1.0
sourcesrc
created_at2024-06-25 16:14:19.617563
updated_at2024-06-25 16:14:19.617563
descriptiona wrapper for the Snapcast JSON-RPC Control API based on tokio
homepagehttps://github.com/JoeyEamigh/snapcast-control
repositoryhttps://github.com/JoeyEamigh/snapcast-control.git
max_upload_size
id1283470
size134,530
Joey Eamigh (JoeyEamigh)

documentation

https://docs.rs/snapcast-control

README

snapcast-control

snapcast-control is a Rust api client for Snapcast. It supports all features of the Snapcast JSON-RPC API as of version 0.28.0 (2024/6/25).

Documentation is available at docs.rs.

Features

  • native rust types for all api requests and responses
  • tokio-based async client
  • client with helper methods for all api requests
  • automatic socket reconnection via stubborn-io

Installation

cargo add snapcast-control

Usage

The best example of this crate's usage is snapcast-multiroom, the project I designed it for.

A simple example of usage:

use snapcast_control::{SnapcastConnection, ValidMessage};

#[tokio::main]
async fn main() {
  let mut client = SnapcastConnection::open("127.0.0.1:1705".parse().expect("could not parse socket address")).await;

  // client state is updated with each message received
  let state = client.state.clone();

  // state is empty initially, sending the server_get_status request will populate it
  client.server_get_status().await.expect("could not send request");

  loop {
    tokio::select! {
      // as messages are received, they are stored in the client's recv buffer
      Some(message) = client.recv() => {
        if let Ok(response) = message {
          // handle response
          match response {
            ValidMessage::Result { id, jsonrpc, result  } => {},
            ValidMessage::Notification { method, jsonrpc } => {},
          }
        } else if let Err(err) = message {
          // handle error
        }
      },
      _ = tokio::signal::ctrl_c() => {
        break;
      }
    }
  }
}
Commit count: 1

cargo fmt