我有一servant臺服務器實作了用于授權的 Web 套接字票務系統。我正在使用servant-websocketsConduit端點。我已經定義了一個輸入訊息:
data WSInput
= Auth {token :: String}
| InMessage {inValue :: Value}
deriving (Show, Generic)
由于票證只會持續幾秒鐘,我的管道將只檢查 Auth 訊息中的第一條訊息。輸出訊息如下:
data WSOutput
= PoisonPill
| AuthOK
| OutMessage {outValue :: Value}
deriving (Show, Generic)
管道可能是這樣的:
wsConduit =
mapMC checkTokenExists
.| takeWhileC
( \case
PoisonPill -> False
otherwise -> True
)
管道只是身份驗證后的回聲。這checkTokenExists是第一條訊息所需要的,實際上是阻塞所有的InMessage,所以我不能把它留在那里。
checkTokenExists我可以在授權后讓通過s有一些狀態,InMessage但對我來說,感覺最好的方法是mapMC checkTokenExists通過filterC讓應用程式訊息完全替換。
如何根據流入的單個元素動態更改管道流的形狀?
uj5u.com熱心網友回復:
“根據輸入改變程式的形狀” = Monad. 出于這個原因,ConduitT是Monad, 帶有操作await和yield。直接說出你的意思:
checkTokenExists :: MonadIO m => WSInput -> m Bool
-- should be along the lines of
wsConduit = do
auth <- checkTokenExists <$> await
if auth then forever $ await >>= yield
else yield PoisonPill
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/487882.html
