use mobiler_core::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone)]
pub enum Msg {
Connect,
WsOpen(PluginResponse),
SendPing,
Sent(PluginResponse),
WsFrame(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 => cx.plugin("websocket", "connect", "wss://echo.websocket.org", Msg::WsOpen),
Msg::WsOpen(resp) => {
model.connected = resp.ok;
if resp.ok {
model.log.push("connected".into());
cx.plugin("websocket", "recv", "", Msg::WsFrame);
} else {
model.log.push(format!("connect failed: {}", resp.output));
}
}
Msg::SendPing => cx.plugin("websocket", "send", "ping", Msg::Sent),
Msg::Sent(_) => model.log.push("sent: ping".into()),
Msg::WsFrame(resp) => {
if resp.ok {
model.log.push(format!("recv: {}", resp.output));
cx.plugin("websocket", "recv", "", Msg::WsFrame);
} else {
model.connected = false;
model.log.push("disconnected".into());
}
}
Msg::Disconnect => cx.plugin("websocket", "close", "", Msg::Noop),
Msg::Noop(_) => {}
}
}
}