Skip to content

Commit

Permalink
Add stopClient to close connection without sending disconnect message
Browse files Browse the repository at this point in the history
  • Loading branch information
albertov authored and dustin committed Jun 30, 2023
1 parent 984354f commit 0112da2
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Network/MQTT/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Network.MQTT.Client (
-- * Running and waiting for the client.
waitForClient,
connectURI, isConnected,
disconnect, normalDisconnect,
disconnect, normalDisconnect, stopClient,
-- * General client interactions.
subscribe, unsubscribe, publish, publishq, pubAliased,
svrProps, connACK, MQTTException(..),
Expand Down Expand Up @@ -373,6 +373,15 @@ waitForClient c@MQTTClient{..} = flip E.finally (stopCallbackThread c) $ do
void . traverse wait =<< readTVarIO _ct
maybe (pure ()) E.throwIO =<< atomically (stateX c Stopped)

-- | Stops the client and closes the connection without sending a DISCONNECT
-- message to the broker. This will cause the last-will message to be delivered
-- by the broker if it has been defined.
stopClient :: MQTTClient -> IO ()
stopClient MQTTClient{..} = do
void . traverse cancel =<< readTVarIO _ct
void . traverse cancel =<< readTVarIO _cbHandle
atomically (writeTVar _st Stopped)

stateX :: MQTTClient -> ConnState -> STM (Maybe E.SomeException)
stateX MQTTClient{..} want = f <$> readTVar _st

Expand Down

0 comments on commit 0112da2

Please sign in to comment.