jaspervdj / websockets

A Haskell library for creating WebSocket-capable servers
http://jaspervdj.be/websockets
BSD 3-Clause "New" or "Revised" License
405 stars 112 forks source link

The behavior where `receiveDataMessage` blocks execution isn't documented. #221

Open techmindful opened 2 years ago

techmindful commented 2 years ago

The documentation of this function here doesn't mention how it blocks execution, until a message is received from the websocket. This can cause confusion.

flip111 commented 1 year ago

Hi i know this question is a bit old but i was wondering the same. So far i found out that receiveDataMessage doesn't block "especially". Yes it's recursive for control message but a normal message it returns. It does block the same receive and receiveData blocks.

Blocking works on makeSocketStream eventually you will get into socket code recvBuf and friends which use functions from base throwErrnoIfRetryMayBlock.

Where it creates the actual blocking functionality (yielding thread control until next packet comes in), i haven't been able to find.

Also note that the code on the front page

meow :: WS.Connection -> IO ()
meow conn = forever $ do
    msg <- WS.receiveData conn
    WS.sendTextData conn $ msg `T.append` ", meow"

has it's own "blocking" going on with forever. That seems to be just a design choice of this library that you have to create your own loops. I'm not sure how you would break out of that forever loop, possibly after closing the connection either the receive or send function will throw an exception. (i assume but didn't test).

Hope this helps