websockets 0.1.2.3 → 0.1.2.5
raw patch · 2 files changed
+61/−54 lines, 2 files
Files
- Network/WebSockets.hs +0/−48
- websockets.cabal +61/−6
Network/WebSockets.hs view
@@ -1,57 +1,9 @@-{- | A library for creating WebSocket-capable servers, where the implemented protocol is defined here: <http://is.gd/eSdLB>.--This library was only tested with Chromium 7.* and Firefox 4.*, currently doesn't implement the entire WebSocket protocol, will never implement older versions of the WebSocket protocol and is a work in progress.--How do you use this library? Here's how:--* Get a 'Handle' to your connected client.--* Perform the initial handshake with 'shakeHands' (or 'getRequest' and 'putResponse').--* Send and receive strict bytestrings with 'putFrame' and 'getFrame'.--And here's a short example of a server that accepts clients, greets them with a welcome message and replies to all messages by echoing them back with an appended meow:--@-import Network.WebSockets ('shakeHands', 'getFrame', 'putFrame')-import Network (listenOn, PortID(PortNumber), accept, withSocketsDo)-import System.IO (Handle, hClose)-import Data.ByteString (append)-import Data.ByteString.UTF8 (fromString) -- this is from utf8-string-import Control.Monad (forever)-import Control.Concurrent (forkIO)--main :: IO ()-main = withSocketsDo $ do- socket <- listenOn $ PortNumber 12345- putStrLn \"Listening on port 12345.\"- forever $ do- (h, _, _) <- accept socket- forkIO $ talkToClient h--talkToClient :: Handle -> IO ()-talkToClient h = do- request <- 'shakeHands' h- case request of- Left error -> putStrLn error >> hClose h- Right req -> do- putStrLn \"Shook hands with client. Commence meowing.\"- 'putFrame' h . fromString $ \"滴水之恩当以涌泉相报\<br\>\" ++ show req- forever $ do- msg <- 'getFrame' h- 'putFrame' h . append msg $ fromString \", MEOW!\"-@--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>. -} module Network.WebSockets ( shakeHands, getRequest, putResponse, getFrame, putFrame, reqHost, reqPath, reqOrigin, reqLocation, Request()) where- import System.IO (Handle, hPutChar, hFlush, hGetChar, hPutStr, hGetLine) import Data.Binary (encode)
websockets.cabal view
@@ -1,14 +1,69 @@ Name: websockets-Version: 0.1.2.3+Version: 0.1.2.5 Cabal-version: >=1.6 -Synopsis: Implements the WebSocket protocol.+Synopsis: Server-side WebSocket protocol handshake and communication. -Description: Implements certain basics of the WebSocket protocol- as defined at <http://is.gd/eSdLB>, allowing the- creation of simple WebSocket-capable servers. Works- with Handles and strict ByteStrings.+Description:+ .+ A library for creating WebSocket-capable servers, where the implemented protocol is defined here: <http://is.gd/eSdLB>.+ .+ This library is only tested with Chromium >=7 and Firefox >=4 and is a work in progress.+ .+ How do you use this library? Here's how:+ .+ * Get a 'Handle' to your connected client.+ .+ * Perform the initial handshake with 'shakeHands' (or 'getRequest' and 'putResponse').+ .+ * Send and receive strict bytestrings with 'putFrame' and 'getFrame'.+ .+ And here's a short example of a server that accepts clients, greets them with a welcome message, checks for disconnects and replies to all messages by echoing them back with an appended meow:+ .+ > 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)+ > + > main :: IO ()+ > main = withSocketsDo $ do+ > socket <- listenOn (PortNumber 12345)+ > putStrLn "Listening on port 12345."+ > forever $ do+ > (h, _, _) <- accept socket+ > forkIO (talkTo h)+ > + > + > talkTo :: Handle -> IO ()+ > talkTo h = do+ > request <- shakeHands h+ > case request of+ > Left error -> putStrLn error+ > Right rqst -> do+ > putFrame h (fromString "滴水之恩当以涌泉相报")+ > putStrLn "Shook hands, sent welcome message."+ > talkLoop h+ > + > hClose h+ > putStrLn "Client quit, closed handle."+ > + > + > talkLoop :: Handle -> IO ()+ > talkLoop h = do+ > msg <- getFrame h+ > if B.null msg+ > then return ()+ > 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>. License: BSD3 License-file: LICENCE