Moonsock is a simple way to connect to a klipper/moonraker 3D printer websocket from anywhere where tokio can be ran and your printer is accessible from the internet.
```rust // I prefer starting my own tokio runtimes for this sort of use-case. println!("Starting Main Runtime"); let moonrt = Builder::newmultithread() .workerthreads(1) .threadname("Moonraker Websocket Main") .enableall() .build() .unwrap(); // Note: the port might be different for you, but 7125 is moonraker's default port. Check your moonraker.conf if you have problems let url = "ws://[printer ip or hostname]:7125/websocket"; let mut connection = moonrt.blockon(MoonConnection::new(URL.to_string(), 1000, 1000));
// This part below needs to be ran in a tokio runtime. Although we do have support for blocking calls on connection for both sending and receiving messages. match connection.recv().await { Some(msg) => println!("Received: {:?}", msg), None => println!("Failed to receive message from connection"), }
let tempid = 42342; let message = MoonMSG::new(MoonMethod::ServerTemperatureStore, None, Some(tempid)); // This will send a message and return the response for that specific message. An id should be set on the sent message or else the future might never yield. match connection.sendlisten(message.clone()).await { Ok(message) => { ... }, Err() => continue, } ```
Not all message types are supported by the parser currently, but most of the important ones are. If you want support for more messages, either wait or create a pull request on the github.