如何为服务方定义服务器发送事件 (SSE) 端点。文档似乎没有涵盖这种情况。
如果 Servant 不是为实时用例设计的,哪个 Haskell 服务器框架支持 SSE?
servant
使用WAI
,并且您始终可以使用组合器深入了解普通WAI
应用程序和所有为它而存在的库Raw
。因此,您可以使用Network.Wai.EventSource
fromwai-extra
创建一个Application
,这是Raw
端点处理程序的类型。就像是:
type MyApi = "normalapi" :> NormalApi
:<|> "sse" :> Raw
myServer :: Server MyAPI
myServer = normalServer :<|> eventSourceAppChan myChan
感谢user2141650的回答,我设法获得了一个使用channels的服务器发送事件的工作示例。
解决方案的要点如下。假设我们有一个只回显消息的回显服务器:
newtype Message = Message { msgText :: Text }
然后我们将定义三个端点,一个用于创建会话,一个用于向会话发送消息,另一个用于使用服务器发送的事件检索会话的消息:
# Create a new session
curl -v -XPOST http://localhost:8081/session/new
# And subscribe to its events
curl -v http://localhost:8081/events/0
# And from another terminal
curl -v -XPOST http://localhost:8081/session/0/echo\
-H "Content-Type: application/json" -d '{"msgText": "Hello"}'
现在让我们看看如何实现端点以将给定会话的消息写入通道:
sendH :: SessionId -> Message -> Handler NoContent
sendH sid msg = do
-- lookupChannel :: Env -> SessionId -> IO (Maybe (Chan ServerEvent))
mCh <- liftIO $ lookupChannel env sid
case mCh of
Nothing ->
throwError err404
Just ch -> do
liftIO $ writeChan ch (asServerEvent msg)
return NoContent
将a转换Message
为a的函数ServerEvent
如下所示:
import Data.Text.Encoding as TE
import qualified Data.Text.Lazy as T
asServerEvent :: Message -> ServerEvent
asServerEvent msg = ServerEvent
{ eventName = Just eName
, eventId = Nothing
, eventData = [msg']
}
where
eName :: Builder
eName = fromByteString "Message arrived"
msg' :: Builder
msg' = fromByteString $ TE.encodeUtf8 $ T.toStrict $ msgText msg
最后,可以使用 来实现从服务器检索消息的处理程序evetSourceAppChan
,如下所示:
eventsH sid = Tagged $ \req respond -> do
mCh <- lookupChannel env sid
case mCh of
Nothing -> do
let msg = "Could not find session with id: "
<> TLE.encodeUtf8 (T.pack (show sid))
respond $ responseLBS status404 [] msg
Just ch -> do
ch' <- dupChan ch
eventSourceAppChan ch req respond
我的sanbox提供了完整的解决方案。
我希望这会有所帮助。
仆人只需一点样板就可以很好地处理这个问题。在这种情况下,您需要一个新的内容类型 ( EventStream
) 和一个支持类来将类型呈现为 SSE 格式。
{-# LANGUAGE NoImplicitPrelude #-}
module Spencer.Web.Rest.ServerSentEvents where
import RIO
import qualified RIO.ByteString.Lazy as BL
import Servant
import qualified Network.HTTP.Media as M
-- imitate the Servant JSON and OctetStream implementations
data EventStream deriving Typeable
instance Accept EventStream where
contentType _ = "text" M.// "event-stream"
instance ToSSE a => MimeRender EventStream a where
mimeRender _ = toSSE
-- imitate the ToJSON type class
class ToSSE a where
toSSE :: a -> BL.ByteString
-- my custom type with simple SSE render
data Hello = Hello
instance ToSSE Hello where
toSSE _ = "data: hello!\n\n"
-- my simple SSE server
type MyApi = "sse" :> StreamGet NoFraming EventStream (SourceIO Hello)
myServer :: Server MyAPI
myServer = source [Hello, Hello, Hello]
浏览器结果:
data: hello!
data: hello!
data: hello!