Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to emit plain text #219

Closed
SSebo opened this issue Aug 14, 2022 · 0 comments
Closed

Failed to emit plain text #219

SSebo opened this issue Aug 14, 2022 · 0 comments

Comments

@SSebo
Copy link
Contributor

SSebo commented Aug 14, 2022

Javascript socket.io client can emit plain text. rust_socketio currently is not capable of.

client.emit('message-received', data); // javascript

For example on will received a payload. can not just emit that payload back when it is plain text.
currently should use json!() macro to encode.

let socket = ClientBuilder::new("http://localhost:4200")
       .namespace("/admin")
       .on("message-received", |p, socket| {
            let s = match &p {
                Payload::String(s) => s,
                _ => "",
            };
            println!("message-received {:?}", s);
            let _ = socket.emit("utf-8™", p); // will failed
        })
       .connect()

because of this function

pub(crate) fn build_packet_for_payload<'a>(
&'a self,
payload: Payload,
event: Event,
nsp: &'a str,
id: Option<i32>,
) -> Result<Packet> {
match payload {
Payload::Binary(bin_data) => Ok(Packet::new(
if id.is_some() {
PacketId::BinaryAck
} else {
PacketId::BinaryEvent
},
nsp.to_owned(),
Some(serde_json::Value::String(event.into()).to_string()),
id,
1,
Some(vec![bin_data]),
)),
Payload::String(str_data) => {
serde_json::from_str::<serde_json::Value>(&str_data)?;
let payload = format!("[\"{}\",{}]", String::from(event), str_data);
Ok(Packet::new(
PacketId::Event,
nsp.to_owned(),
Some(payload),
id,
0,
None,
))
}
}

this line

serde_json::from_str::<serde_json::Value>(&str_data)?;

@SSebo SSebo changed the title Failed to emit what received if the payload is plain text Failed to emit plain text Aug 14, 2022
@SSebo SSebo closed this as completed Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant