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

Let handle_event provide Vec<Value> to self.callback #394

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions socketio/src/asynchronous/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl Client {
};

// a socketio message always comes in one of the following two flavors (both JSON):
// 1: `["event", "msg"]`
// 1: `["event", "msg", ...]`
// 2: `["msg"]`
// in case 2, the message is ment for the default message event, in case 1 the event
// is specified
Expand All @@ -340,17 +340,28 @@ impl Client {
_ => Event::Message,
};

(event, contents.get(1).ok_or(Error::IncompletePacket())?)
let msg = if let Some((_, payload)) = contents.split_first() {
payload.to_vec()
} else {
return Err(Error::IncompletePacket());
};

(event, msg)
} else {
// case 2
// FIXME: `["msg", "msg", ...]` could technically happen I believe?
// Case 2 could still possibly return less data than desired
(
Event::Message,
contents.first().ok_or(Error::IncompletePacket())?,
vec![contents
.first()
.ok_or(Error::IncompletePacket())?
.to_owned()],
)
};

// call the correct callback
self.callback(&event, data.to_string()).await?;
self.callback(&event, data).await?;
}

Ok(())
Expand Down
19 changes: 15 additions & 4 deletions socketio/src/client/raw_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl RawClient {
};

// a socketio message always comes in one of the following two flavors (both JSON):
// 1: `["event", "msg"]`
// 1: `["event", "msg", ...]`
// 2: `["msg"]`
// in case 2, the message is ment for the default message event, in case 1 the event
// is specified
Expand All @@ -339,17 +339,28 @@ impl RawClient {
_ => Event::Message,
};

(event, contents.get(1).ok_or(Error::IncompletePacket())?)
let msg = if let Some((_, payload)) = contents.split_first() {
payload.to_vec()
} else {
return Err(Error::IncompletePacket());
};

(event, msg)
} else {
// case 2
// FIXME: `["msg", "msg", ...]` could technically happen I believe?
// Case 2 could still possibly return less data than desired
(
Event::Message,
contents.first().ok_or(Error::IncompletePacket())?,
vec![contents
.first()
.ok_or(Error::IncompletePacket())?
.to_owned()],
)
};

// call the correct callback
self.callback(&event, data.to_string())?;
self.callback(&event, data)?;
}

Ok(())
Expand Down