servant-websockets icon indicating copy to clipboard operation
servant-websockets copied to clipboard

servant-client support

Open joeyh opened this issue 7 months ago • 0 comments

I have, I think, implemented servant-client support for websockets. At least, this code compiles..

data WebSocketClient = WebSocketClient Request

instance RunClient m => HasClient m WebSocket where
          type Client m WebSocket = WebSocketClient
          clientWithRoute _pm Proxy req = WebSocketClient req
          hoistClientMonad _ _ _ w = w

That's enough to allow using client to extract a WebSocket route. Rather than the usual ClientM runner, it will produce a WebSocketClient. To run that, I wrote this function:

runWebSocketClient :: WebSocketClient -> Websocket.ClientApp a -> ClientM a
runWebSocketClient (WebSocketClient req) app = do
        clientenv <- ask
        let burl = baseUrl clientenv
        let creq = defaultMakeClientRequest burl req
        case baseUrlScheme burl of
                Http -> liftIO $ Websocket.runClient
                        (baseUrlHost burl)
                        (baseUrlPort burl)
                        (decodeBS (path creq))
                        app
                Https -> error "TODO" -- needs wuss

joeyh avatar Jul 08 '24 11:07 joeyh