use mobiler_core::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone)]
pub enum Msg {
Connect,
Frame(PluginResponse),
SendPing,
Sent(PluginResponse),
Disconnect,
Noop(PluginResponse),
}
#[derive(Default, Serialize, Deserialize)]
pub struct Model {
pub connected: bool,
pub log: Vec<String>,
}
impl MyApp {
fn handle(&self, msg: Msg, model: &mut Model, cx: &mut Cx<Msg>) {
match msg {
Msg::Connect => {
model.connected = true;
cx.subscribe("ws", "websocket", "stream", "wss://echo.websocket.org", Msg::Frame);
}
Msg::Frame(resp) => {
if resp.ok {
model.log.push(format!("recv: {}", resp.output));
} else {
model.connected = false;
model.log.push("disconnected".into());
}
}
Msg::SendPing => cx.plugin("websocket", "send", "ping", Msg::Sent),
Msg::Sent(_) => model.log.push("sent: ping".into()),
Msg::Disconnect => {
cx.unsubscribe("ws");
model.connected = false;
}
Msg::Noop(_) => {}
}
}
}