Skip to content

Commit

Permalink
ws::res > Message add field if & add respond fn to zws to inject msg id
Browse files Browse the repository at this point in the history
  • Loading branch information
canewsin committed Jan 20, 2024
1 parent 3c12a24 commit 18820b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/plugins/websocket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ fn handle_error(
}

impl ZeruWebsocket {
fn respond(
&mut self,
ctx: &mut ws::WebsocketContext<ZeruWebsocket>,
msg: &mut Message,
) -> Result<(), Error> {
msg.id = self.next_message_id;
self.next_message_id += 1;
let j = serde_json::to_string(&msg)?;
ctx.text(j);
Ok(())
}

fn is_admin_site(&mut self) -> Result<bool, Error> {
let site = block_on(self.site_addr.send(SiteInfoRequest {}))??;
let res = site
Expand Down Expand Up @@ -355,11 +367,8 @@ impl ZeruWebsocket {
// error: "Unhandled Plugin command".to_string(),
// });
};

let j = serde_json::to_string(&response?)?;
ctx.text(j);

Ok(())
let mut msg = response?;
self.respond(ctx, &mut msg)
}

fn update_websocket(&mut self, params: Option<serde_json::Value>) {
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/websocket/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::utils::is_default;
pub struct Message {
cmd: MessageType,
#[serde(skip_serializing_if = "is_default")]
pub id: usize,
#[serde(skip_serializing_if = "is_default")]
to: isize,
result: serde_json::Value,
}
Expand All @@ -25,6 +27,7 @@ impl Message {
cmd: MessageType::Response,
to: id,
result: body,
id: 0,
}
}

Expand All @@ -33,6 +36,7 @@ impl Message {
cmd: MessageType::InjectScript,
to: id,
result: body,
id: 0,
}
}
}

0 comments on commit 18820b8

Please sign in to comment.