-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
286 additions
and
1,948 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Copyright (c) 2011, Palmik, yihuang | ||
Copyright (c) 2011, Palmik | ||
|
||
All rights reserved. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
module Network.Wai.Sock.Application | ||
( Application | ||
( Application(..) | ||
, ApplicationSettings(..) | ||
) where | ||
|
||
------------------------------------------------------------------------------ | ||
import qualified Data.ByteString.Lazy as LB (ByteString) | ||
import qualified Data.ByteString as SB (ByteString) | ||
import qualified Data.Conduit as C (Source, Sink) | ||
import qualified Data.ByteString.Lazy as BL (ByteString) | ||
import qualified Data.Conduit as C (Source, Sink) | ||
import qualified Data.Text as TS (Text) | ||
------------------------------------------------------------------------------ | ||
|
||
type Application m = C.Source m LB.ByteString -> C.Sink LB.ByteString m () -> m () | ||
data Application m = Application | ||
{ applicationDefinition :: C.Source m BL.ByteString -> C.Sink BL.ByteString m () -> m () | ||
, applicationSettings :: ApplicationSettings | ||
} | ||
|
||
data ApplicationSettings = ApplicationSettings | ||
{ appSettingsPrefix :: [TS.Text] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
module Network.Wai.Sock.Environment | ||
( Environment | ||
) where | ||
|
||
------------------------------------------------------------------------------ | ||
import Control.Applicative | ||
import Control.Concurrent.MVar.Lifted | ||
import Control.Monad.Base | ||
import Control.Monad.Trans.Control | ||
------------------------------------------------------------------------------ | ||
import qualified Data.ByteString.Lazy as LB (ByteString) | ||
import qualified Data.Conduit as C (Source, Sink) | ||
import qualified Data.HashMap.Strict as HM (HashMap, insert, lookup) | ||
------------------------------------------------------------------------------ | ||
import Network.Wai.Sock.Session | ||
------------------------------------------------------------------------------ | ||
|
||
newtype Environment = Environment | ||
{ envSessions :: MVar (HM.HashMap SessionID (MVar Session)) | ||
} | ||
|
||
addSession :: MonadBaseControl IO m | ||
=> SessionID | ||
-> Session | ||
-> Environment | ||
-> m () | ||
addSession sid s Environment{..} = do | ||
ms <- newMVar s | ||
modifyMVar_ envSessions (return . HM.insert sid ms) | ||
|
||
-- | Applies the given function on session with given ID and saves the new value. | ||
-- If session with the supplied ID does not exist, it's virtually no-op. | ||
modifySession :: MonadBaseControl IO m | ||
=> (Session -> m Session) | ||
-> SessionID | ||
-> Environment | ||
-> m () | ||
modifySession f sid Environment{..} = withMVar envSessions go | ||
where go smap = case HM.lookup sid smap of | ||
Just ms -> modifyMVar_ ms f | ||
Nothing -> return () | ||
|
||
|
||
-- | Retrieves session with the given ID, if there is no such session, it's created first. | ||
getSession :: MonadBaseControl IO m | ||
=> SessionID | ||
-> Environment | ||
-> m (MVar Session) | ||
getSession sid Environment{..} = modifyMVar envSessions go | ||
where go smap = case HM.lookup sid smap of | ||
Just ms -> return (smap, ms) | ||
Nothing -> do | ||
ms <- newSession sid >>= newMVar | ||
return (HM.insert sid ms smap, ms) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Network.Wai.Sock.Frame | ||
( Frame(..) | ||
) where | ||
|
||
------------------------------------------------------------------------------ | ||
import qualified Data.Text as TS (Text, unpack) | ||
------------------------------------------------------------------------------ | ||
|
||
data Frame | ||
= FrameOpen | ||
| FrameHeartbeat | ||
| FrameMessage TS.Text | ||
| FrameClose Int TS.Text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Network.Wai.Sock.Server | ||
( ServerSettings(..) | ||
) where | ||
|
||
------------------------------------------------------------------------------ | ||
import qualified Data.Text as TS (Text) | ||
import Data.Default | ||
------------------------------------------------------------------------------ | ||
|
||
data ServerSettings = ServerSettings | ||
{ serverSettingsWebsocketsEnabled :: Bool | ||
, serverSettingsCookiesNeeded :: Bool | ||
, serverSettingsAllowedOrigins :: TS.Text | ||
, serverSettingsSockURL :: TS.Text | ||
, serverSettingsSockVersion :: TS.Text | ||
} | ||
|
||
instance Default ServerSettings where | ||
def = ServerSettings | ||
{ serverSettingsWebsocketsEnabled = True | ||
, serverSettingsCookiesNeeded = True | ||
, serverSettingsAllowedOrigins = "*:*" | ||
, serverSettingsSockURL = "http://cdn.sockjs.org/sockjs-0.3.min.js" | ||
, serverSettingsSockVersion = "0.3" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
|
||
module Network.Wai.Sock.Session | ||
( Session(..) | ||
, SessionStatus(..) | ||
, SessionID | ||
|
||
, newSession | ||
) where | ||
|
||
------------------------------------------------------------------------------ | ||
import Control.Applicative | ||
import Control.Concurrent.Chan.Lifted | ||
import Control.Monad.Base | ||
------------------------------------------------------------------------------ | ||
import qualified Data.ByteString.Lazy as BL (ByteString) | ||
import qualified Data.ByteString as BS (ByteString) | ||
import qualified Data.Conduit as C (Source, Sink) | ||
import qualified Data.Text as TS (Text) | ||
------------------------------------------------------------------------------ | ||
|
||
newSession :: MonadBase IO m | ||
=> SessionID | ||
-> m Session | ||
newSession sid = Session sid SessionFresh <$> newChan | ||
|
||
data Session = Session | ||
{ sessionID :: SessionID | ||
, sessionStatus :: SessionStatus | ||
, sessionIncomingBuffer :: Chan BS.ByteString | ||
} | ||
|
||
-- | SessionID | ||
type SessionID = TS.Text | ||
|
||
-- | SessionStatus | ||
data SessionStatus | ||
= SessionFresh -- ^ Right after creation, Session is "Fresh" | ||
| SessionOpened -- ^ Right after we send opening frame, Session is "Opened". We also start the timeout & heartbeat timer at this point. | ||
| SessionClosed -- ^ Right after we send closing frame, Session if "Closed". |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.