Skip to content

Commit

Permalink
Merge pull request #44 from 1c3t3a/issue-43
Browse files Browse the repository at this point in the history
Added a disconnect method for the socket.io socket
  • Loading branch information
1c3t3a authored May 23, 2021
2 parents 2b950b6 + 9448467 commit f186922
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 18 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup rust environment
- name: Setup rust environment
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
profile: minimal
toolchain: stable
override: true

- name: Setup docker
id: buildx
Expand All @@ -33,7 +33,8 @@ jobs:
- name: Run cargo-tarpaulin
uses: actions-rs/tarpaulin@v0.1
with:
version: '0.15.0'
version: "0.18.0-alpha3"
out-type: Lcov

- name: Upload to codecov.io
uses: codecov/codecov-action@v1.0.2
Expand Down
18 changes: 8 additions & 10 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
coverage:
range: 50..90 # coverage lower than 50 is red, higher than 90 green, between color code

status:
project:
project: # settings affecting project coverage
default:
target: auto
threshold: 2%
paths:
- "src"
branches:
- main
if_ci_failed: ignore
informational: false
only_pulls: false
target: auto # auto % coverage target
threshold: 5% # allow for 5% reduction of coverage without failing

# do not run coverage on patch nor changes
patch: false
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,33 @@ impl Socket {
self.transport.emit(event.into(), data.into())
}

/// Disconnects this client from the server by sending a `socket.io` closing
/// packet.
/// # Example
/// ```rust
/// use rust_socketio::{SocketBuilder, Payload};
/// use serde_json::json;
///
/// let mut socket = SocketBuilder::new("http://localhost:4200")
/// .on("test", |payload: Payload, mut socket| {
/// println!("Received: {:#?}", payload);
/// socket.emit("test", json!({"hello": true})).expect("Server unreachable");
/// })
/// .connect()
/// .expect("connection failed");
///
/// let json_payload = json!({"token": 123});
///
/// socket.emit("foo", json_payload);
///
/// // disconnect from the server
/// socket.disconnect();
///
/// ```
pub fn disconnect(&mut self) -> Result<()> {
self.transport.disconnect()
}

/// Sends a message to the server but `alloc`s an `ack` to check whether the
/// server responded in a given timespan. This message takes an event, which
/// could either be one of the common events like "message" or "error" or a
Expand Down Expand Up @@ -492,6 +519,9 @@ mod test {
);
assert!(ack.is_ok());

socket.disconnect().unwrap();
// assert!(socket.disconnect().is_ok());

sleep(Duration::from_secs(4));
}

Expand Down
34 changes: 31 additions & 3 deletions src/socketio/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ impl TransportClient {
.lock()?
.bind(self.host.as_ref().to_string())?;

let default = String::from("/");
// construct the opening packet
let open_packet = SocketPacket::new(
SocketPacketId::Connect,
self.nsp.as_ref().as_ref().unwrap_or(&default).to_owned(),
self.nsp
.as_ref()
.as_ref()
.unwrap_or(&String::from("/"))
.to_owned(),
None,
None,
None,
Expand All @@ -112,10 +115,35 @@ impl TransportClient {
self.send(&open_packet)
}

/// Disconnects from the server by sending a socket.io `Disconnect` packet. This results
/// in the underlying engine.io transport to get closed as well.
pub fn disconnect(&mut self) -> Result<()> {
if !self.is_engineio_connected()? || !self.connected.load(Ordering::Acquire) {
return Err(Error::IllegalActionAfterOpen);
}

let disconnect_packet = SocketPacket::new(
SocketPacketId::Disconnect,
self.nsp
.as_ref()
.as_ref()
.unwrap_or(&String::from("/"))
.to_owned(),
None,
None,
None,
None,
);

self.send(&disconnect_packet)?;
self.connected.store(false, Ordering::Release);
Ok(())
}

/// Sends a `socket.io` packet to the server using the `engine.io` client.
pub fn send(&self, packet: &SocketPacket) -> Result<()> {
if !self.is_engineio_connected()? || !self.connected.load(Ordering::Acquire) {
return Err(Error::ActionBeforeOpen);
return Err(Error::IllegalActionAfterOpen);
}

// the packet, encoded as an engine.io message packet
Expand Down

0 comments on commit f186922

Please sign in to comment.