Skip to content

Commit 8605ca8

Browse files
committed
feat(socketio): send close reason when emitting Event::Close
Previously, when we emitted an Event::Close, we didn't specify a reason for why this event was emitted. When implementing the close notification on unexpected transport closes, we started sending 'transport close' as the payload for the close event. We now decided to do this more consistently and added a close reason when emitting the Event::Close, so that the client better knows why this event was emitted.
1 parent d2da53a commit 8605ca8

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

socketio/src/asynchronous/client/client.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
asynchronous::socket::Socket as InnerSocket,
2020
error::{Error, Result},
2121
packet::{Packet, PacketId},
22-
Event, Payload,
22+
CloseReason, Event, Payload,
2323
};
2424

2525
#[derive(Default)]
@@ -168,7 +168,7 @@ impl Client {
168168
// We don't need to do that in the other cases, since proper server close
169169
// and manual client close are handled explicitly.
170170
if let Some(err) = client_clone
171-
.callback(&Event::Close, "transport close")
171+
.callback(&Event::Close, CloseReason::TransportClose.as_str())
172172
.await
173173
.err()
174174
{
@@ -524,7 +524,8 @@ impl Client {
524524
}
525525
PacketId::Disconnect => {
526526
*(self.disconnect_reason.write().await) = DisconnectReason::Server;
527-
self.callback(&Event::Close, "").await?;
527+
self.callback(&Event::Close, CloseReason::IOServerDisconnect.as_str())
528+
.await?;
528529
}
529530
PacketId::ConnectError => {
530531
self.callback(
@@ -604,8 +605,7 @@ mod test {
604605
},
605606
error::Result,
606607
packet::{Packet, PacketId},
607-
Event,
608-
Payload, TransportType,
608+
CloseReason, Event, Payload, TransportType,
609609
};
610610

611611
#[tokio::test]
@@ -977,7 +977,10 @@ mod test {
977977
let rx_timeout = timeout(Duration::from_secs(1), rx.recv()).await;
978978
assert!(rx_timeout.is_ok());
979979

980-
assert_eq!(rx_timeout.unwrap(), Some(Payload::from("transport close")));
980+
assert_eq!(
981+
rx_timeout.unwrap(),
982+
Some(Payload::from(CloseReason::TransportClose.as_str()))
983+
);
981984

982985
Ok(())
983986
}

socketio/src/client/raw_client.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::callback::Callback;
22
use crate::packet::{Packet, PacketId};
33
use crate::Error;
4-
pub(crate) use crate::{event::Event, payload::Payload};
4+
pub(crate) use crate::{event::CloseReason, event::Event, payload::Payload};
55
use rand::{thread_rng, Rng};
66
use serde_json::Value;
77

@@ -149,7 +149,7 @@ impl RawClient {
149149
let _ = self.socket.send(disconnect_packet);
150150
self.socket.disconnect()?;
151151

152-
let _ = self.callback(&Event::Close, ""); // trigger on_close
152+
let _ = self.callback(&Event::Close, CloseReason::IOClientDisconnect.as_str()); // trigger on_close
153153
Ok(())
154154
}
155155

@@ -372,7 +372,7 @@ impl RawClient {
372372
self.callback(&Event::Connect, "")?;
373373
}
374374
PacketId::Disconnect => {
375-
self.callback(&Event::Close, "")?;
375+
self.callback(&Event::Close, CloseReason::IOServerDisconnect.as_str())?;
376376
}
377377
PacketId::ConnectError => {
378378
self.callback(

socketio/src/event.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,32 @@ impl Display for Event {
5757
f.write_str(self.as_str())
5858
}
5959
}
60+
61+
#[derive(Debug, PartialEq, PartialOrd, Clone, Eq, Hash)]
62+
pub enum CloseReason {
63+
IOServerDisconnect,
64+
IOClientDisconnect,
65+
TransportClose,
66+
}
67+
68+
impl CloseReason {
69+
pub fn as_str(&self) -> &str {
70+
match self {
71+
CloseReason::IOServerDisconnect => "io server disconnect",
72+
CloseReason::IOClientDisconnect => "io client disconnect",
73+
CloseReason::TransportClose => "transport close",
74+
}
75+
}
76+
}
77+
78+
impl From<CloseReason> for String {
79+
fn from(event: CloseReason) -> Self {
80+
Self::from(event.as_str())
81+
}
82+
}
83+
84+
impl Display for CloseReason {
85+
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
86+
f.write_str(self.as_str())
87+
}
88+
}

socketio/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub mod asynchronous;
191191

192192
pub use error::Error;
193193

194-
pub use {event::Event, payload::Payload};
194+
pub use {event::CloseReason, event::Event, payload::Payload};
195195

196196
pub use client::{ClientBuilder, RawClient, TransportType};
197197

0 commit comments

Comments
 (0)