Skip to content

Commit cf9e93e

Browse files
sirkrypt01c3t3a
authored andcommitted
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 dadd1f1 commit cf9e93e

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-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

@@ -376,7 +376,7 @@ impl RawClient {
376376
self.callback(&Event::Connect, "")?;
377377
}
378378
PacketId::Disconnect => {
379-
self.callback(&Event::Close, "")?;
379+
self.callback(&Event::Close, CloseReason::IOServerDisconnect.as_str())?;
380380
}
381381
PacketId::ConnectError => {
382382
self.callback(

socketio/src/event.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,38 @@ impl Display for Event {
5757
f.write_str(self.as_str())
5858
}
5959
}
60+
61+
/// A `CloseReason` is the payload of the [`Event::Close`] and specifies the reason for
62+
/// why it was fired.
63+
/// These are aligned with the official Socket.IO disconnect reasons, see
64+
/// https://socket.io/docs/v4/client-socket-instance/#disconnect
65+
#[derive(Debug, PartialEq, PartialOrd, Clone, Eq, Hash)]
66+
pub enum CloseReason {
67+
IOServerDisconnect,
68+
IOClientDisconnect,
69+
TransportClose,
70+
}
71+
72+
impl CloseReason {
73+
pub fn as_str(&self) -> &str {
74+
match self {
75+
// Inspired by https://github.com/socketio/socket.io/blob/d0fc72042068e7eaef448941add617f05e1ec236/packages/socket.io-client/lib/socket.ts#L865
76+
CloseReason::IOServerDisconnect => "io server disconnect",
77+
// Inspired by https://github.com/socketio/socket.io/blob/d0fc72042068e7eaef448941add617f05e1ec236/packages/socket.io-client/lib/socket.ts#L911
78+
CloseReason::IOClientDisconnect => "io client disconnect",
79+
CloseReason::TransportClose => "transport close",
80+
}
81+
}
82+
}
83+
84+
impl From<CloseReason> for String {
85+
fn from(event: CloseReason) -> Self {
86+
Self::from(event.as_str())
87+
}
88+
}
89+
90+
impl Display for CloseReason {
91+
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
92+
f.write_str(self.as_str())
93+
}
94+
}

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)