moesenle / servant-websockets

BSD 3-Clause "New" or "Revised" License
22 stars 10 forks source link

servant-client support #15

Open joeyh opened 4 months ago

joeyh commented 4 months ago

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