Search code examples
haskellwebsockethaskell-snap-framework

Getting client IP address in websocket library


I am using Network.WebSocket library. I tried WS.requestHeaders . WS.pendingRequest on PendingConnection to get the connection request information. However, it seems it doesn't have client IP information, only the server IP information in Host key. An example output below:

[("Upgrade","websocket"),("Sec-WebSocket-Extensions","permessage-deflate; 
client_max_window_bits"),("Sec-WebSocket-Key","<taken out for SO post>"),
("Connection","Upgrade"),("Sec-WebSocket-Version","13"),("Host","localhost:8080")]

What will be a good way to get the "Client IP" information when running Network.Websocket (I am running it within Snap monad using runWebSocketsSnap). I will like to log it in case of error.


Solution

  • runWebSocketsSnap is just a regular Snap application, so you can use rqRemoteAddr to get remote IP address:

    do
      addr <- getsRequest rqRemoteAddr
      runWebSocketsSnap $ \pending -> ...