Skip to content

Commit

Permalink
Merge pull request #129 from Totodore/ft-improve-connect-process
Browse files Browse the repository at this point in the history
feat(socketio/ns): improve connect process
  • Loading branch information
Totodore authored Oct 25, 2023
2 parents a144daa + d046fab commit d2f5220
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
10 changes: 1 addition & 9 deletions socketioxide/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,14 @@ impl<A: Adapter> Client<A> {
debug!("auth: {:?}", auth);
let sid = esocket.id;
if let Some(ns) = self.get_ns(&ns_path) {
let protocol: ProtocolVersion = esocket.protocol.into();
ns.connect(sid, esocket.clone(), auth, self.config.clone())?;

// cancel the connect timeout task for v5
#[cfg(feature = "v5")]
if let Some(tx) = esocket.data.connect_recv_tx.lock().unwrap().take() {
tx.send(()).unwrap();
}

let connect_packet = Packet::connect(ns_path, sid, protocol);
if let Err(err) = esocket.emit(connect_packet.try_into()?) {
debug!("sending error during socket connection: {err:?}");
}
ns.connect(sid, esocket.clone(), auth, self.config.clone())?;
if let Some(tx) = esocket.data.connect_recv_tx.lock().unwrap().take() {
tx.send(()).unwrap();
}
Ok(())
} else if ProtocolVersion::from(esocket.protocol) == ProtocolVersion::V4 && ns_path == "/" {
error!(
Expand Down
16 changes: 13 additions & 3 deletions socketioxide/src/ns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
adapter::Adapter,
errors::Error,
handler::{BoxedNamespaceHandler, CallbackHandler},
packet::PacketData,
packet::{Packet, PacketData},
socket::Socket,
SocketIoConfig,
};
Expand Down Expand Up @@ -47,9 +47,19 @@ impl<A: Adapter> Namespace<A> {
auth: Option<String>,
config: Arc<SocketIoConfig>,
) -> Result<(), serde_json::Error> {
let socket: Arc<Socket<A>> = Socket::new(sid, self.clone(), esocket, config).into();
let socket: Arc<Socket<A>> = Socket::new(sid, self.clone(), esocket.clone(), config).into();

self.sockets.write().unwrap().insert(sid, socket.clone());
self.handler.call(socket, auth)

let protocol = esocket.protocol.into();
if let Err(e) = socket.send(Packet::connect(self.path.clone(), socket.id, protocol)) {
tracing::error!("error sending connect packet: {:?}, closing conn", e);
esocket.close(engineioxide::DisconnectReason::PacketParsingError);
return Ok(());
}

self.handler.call(socket, auth)?;
Ok(())
}

/// Remove a socket from a namespace and propagate the event to the adapter
Expand Down

0 comments on commit d2f5220

Please sign in to comment.