-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Create WebSocket servers -- -- A library for creating WebSocket-capable servers, where the -- implemented protocol is defined here: http://is.gd/eSdLB. -- -- This library is tested with Chromium >=7, Opera >=11 and Firefox -- >=4 and is a work in progress. (Note: many of these newest browsers -- have recently stopped shipping with websockets enabled by default, due -- to security issues which are yet to be resolved.) -- -- An example of usage can be found in the Network.WebSockets module -- documentation. @package websockets @version 0.3.0.0 -- | How do you use this library? Here's how: -- --
-- import Network.WebSockets (shakeHands, getFrame, putFrame) -- import Network (listenOn, PortID(PortNumber), accept, withSocketsDo) -- import System.IO (Handle, hClose) -- import qualified Data.ByteString as B (append, null) -- import Data.ByteString.UTF8 (fromString) -- this is from utf8-string -- import Control.Monad (forever) -- import Control.Concurrent (forkIO) -- -- -- Accepts clients, spawns a single handler for each one. -- main :: IO () -- main = withSocketsDo $ do -- socket <- listenOn (PortNumber 8088) -- putStrLn "Listening on port 8088." -- forever $ do -- (h, _, _) <- accept socket -- forkIO (talkTo h) -- -- -- Shakes hands with client. If no error, starts talking. -- talkTo :: Handle -> IO () -- talkTo h = do -- request <- shakeHands h -- case request of -- Left err -> print err -- Right _ -> do -- putFrame h (fromString "Do you read me, Lieutenant Bowie?") -- putStrLn "Shook hands, sent welcome message." -- talkLoop h -- -- -- Talks to the client (by echoing messages back) until EOF. -- talkLoop :: Handle -> IO () -- talkLoop h = do -- msg <- getFrame h -- if B.null msg -- then do -- putStrLn "EOF encountered. Closing handle." -- hClose h -- else do -- putFrame h $ B.append msg (fromString ", meow.") -- talkLoop h ---- -- The example above will suffice if you wish to accept any -- WebSocket-capable client, regardless of its origin or target. It won't -- suffice if you have to filter the incoming clients by the contents of -- their requests. For that, you can use getRequest and -- putResponse, which allow you to inspect the request details -- before you send back a response, if any. -- -- If you have any suggestions, bug reports and/or fixes, feel free to -- send them to mailto:sinisa@bidin.cc. Thanks! module Network.WebSockets -- | Accept and perform a handshake, no matter the request contents. -- -- As long as the request is well-formed, the client will receive a -- response saying, essentially, "proceed". Use this function if you -- don't care who you're connected to, as long as that someone speaks the -- WebSocket protocol. -- -- The function returns either a HandshakeError in case of error, -- or a Request on success. The Request is returned purely -- for logging purposes, since the handshake has already been executed. -- Use this function immediately after establishing the connection. -- -- If you wish not to blindly accept requests but to filter them -- according to their contents, use the getRequest and -- putResponse functions. shakeHands :: Handle -> IO (Either HandshakeError Request) -- | Reads the client's opening handshake and returns either a -- Request based on its contents, or a HandshakeError in -- case of an error. getRequest :: Handle -> IO (Either HandshakeError Request) -- | Sends an accepting response based on the given Request, thus -- accepting and ending the handshake. putResponse :: Handle -> Request -> IO () -- | Returns an accepting response based on the given Request. -- putResponse uses this function internally. createResponse :: Request -> ByteString -- | Receive a strict ByteString. Call this function only after having -- performed the handshake. This function will block until an entire -- frame is read. If the writing end of the handle is closed, the -- function returns an empty ByteString. getFrame :: Handle -> IO ByteString -- | Send a strict ByteString. Call this function only after having -- performed the handshake. putFrame :: Handle -> ByteString -> IO () -- | Constructs the response token by using the two security keys the and -- eight-byte token given in the request, as defined by the protocol. createToken :: Request -> ByteString -- | Contains the request details. data Request Request :: String -> String -> String -> String -> String -> String -> Request -- | The requested host. reqHost :: Request -> String -- | The requested path. reqPath :: Request -> String -- | The origin of the request. reqOrigin :: Request -> String -- | The first security key. reqKey1 :: Request -> String -- | The second security key. reqKey2 :: Request -> String -- | The given eight-byte token. reqToken :: Request -> String -- | Error in case of failed handshake. data HandshakeError HsIOError :: String -> HandshakeError HsInvalidGETRequest :: String -> HandshakeError HsInvalidHeaderLine :: String -> HandshakeError HsMissingHeaderKeys :: String -> HandshakeError HsBadFirstSecurityKey :: String -> HandshakeError HsBadSecondSecurityKey :: String -> HandshakeError instance Show HandshakeError instance Show Request