websockets: Server-side WebSocket protocol handshake and communication.

[ bsd3, library, network ] [ Propose Tags ]

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.

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.1.2, 0.1.1.3, 0.1.1.5, 0.1.2.2, 0.1.2.3, 0.1.2.5, 0.2.0.1, 0.3.0.0, 0.3.1.0, 0.3.1.1, 0.4.0.0, 0.4.0.1, 0.4.1.0, 0.5.0.0, 0.5.1.0, 0.5.1.1, 0.5.2.0, 0.5.2.1, 0.6.0.0, 0.6.0.1, 0.6.0.2, 0.6.0.3, 0.6.0.4, 0.7.0.0, 0.7.0.1, 0.7.1.0, 0.7.1.1, 0.7.2.0, 0.7.2.1, 0.7.3.0, 0.7.4.0, 0.7.4.1, 0.8.0.0, 0.8.1.0, 0.8.1.1, 0.8.1.2, 0.8.1.3, 0.8.2.0, 0.8.2.1, 0.8.2.2, 0.8.2.3, 0.8.2.4, 0.8.2.5, 0.8.2.6, 0.9.0.0, 0.9.0.1, 0.9.1.0, 0.9.2.0, 0.9.2.1, 0.9.2.2, 0.9.3.0, 0.9.3.1, 0.9.4.0, 0.9.5.0, 0.9.6.0, 0.9.6.1, 0.9.6.2, 0.9.7.0, 0.9.8.0, 0.9.8.1, 0.9.8.2, 0.10.0.0, 0.11.0.0, 0.11.1.0, 0.11.2.0, 0.12.0.0, 0.12.1.0, 0.12.2.0, 0.12.3.0, 0.12.3.1, 0.12.4.0, 0.12.4.1, 0.12.5.0, 0.12.5.1, 0.12.5.2, 0.12.5.3, 0.12.6.0, 0.12.6.1, 0.12.7.0, 0.12.7.1, 0.12.7.2, 0.12.7.3, 0.13.0.0 (info)
Dependencies base (>=4 && <5), binary (>=0.5), bytestring (>=0.9), containers (>=0.3), network (>=2.2.1.7), pureMD5 (>=2.1) [details]
License BSD-3-Clause
Copyright (c) 2010 Siniša Biđin
Author Siniša Biđin
Maintainer Siniša Biđin <sinisa@bidin.cc>
Category Network
Bug tracker mailto:sinisa@bidin.cc
Uploaded by SinisaBidin at 2010-11-10T20:58:08Z
Distributions Arch:0.12.7.3, Debian:0.12.7.1, Fedora:0.12.7.3, LTSHaskell:0.12.7.3, NixOS:0.12.7.3, Stackage:0.13.0.0
Reverse Dependencies 121 direct, 3427 indirect [details]
Downloads 111403 total (435 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]