Skip to content

Commit 3ce957a

Browse files
committed
it works, well kinda
1 parent 6f0c69c commit 3ce957a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

SocketIO.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ library
6969
build-depends: base >=4.6 && <5.0
7070
, text >=1.1 && <2.0
7171
, bytestring >=0.10 && <1.0
72+
, blaze-builder >=0.3 && <1.0
7273
, aeson >=0.7 && <1.0
7374
, conduit >=1.1 && <2.0
7475
, conduit-extra >=1.1 && <2.0
@@ -102,6 +103,7 @@ test-suite socketio-test
102103
build-depends: base >=4.6 && <5.0
103104
, text >=1.1 && <2.0
104105
, bytestring >=0.10 && <1.0
106+
, blaze-builder >=0.3 && <1.0
105107
, aeson >=0.7 && <1.0
106108
, conduit >=1.1 && <2.0
107109
, conduit-extra >=1.1 && <2.0

Web/SocketIO/Server.hs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import Web.SocketIO.Types
1616

1717
--------------------------------------------------------------------------------
1818
import Control.Monad.Trans (liftIO)
19+
import Blaze.ByteString.Builder (Builder)
20+
import qualified Blaze.ByteString.Builder as Builder
21+
import Data.Conduit
22+
import qualified Data.Conduit.List as CL
23+
import qualified Data.ByteString as B
1924
import Network.HTTP.Types (Status, status200, status403)
2025
import Network.HTTP.Types.Header (ResponseHeaders)
2126
import qualified Network.Wai as Wai
@@ -45,6 +50,26 @@ serverConfig port config handler = do
4550
-- run it with Warp
4651
Warp.run port (httpApp vorspann (runConnection env))
4752

53+
54+
banana :: Wai.Request -> (Request -> IO Message) -> Source IO (Flush Builder)
55+
banana request runConnection' = sourceRequest request $= CL.mapM runConnection' =$= serializeMessage =$= toFlushBuilder
56+
where serializeMessage = do
57+
m <- await
58+
n <- await
59+
case (m, n) of
60+
(Nothing, Nothing) -> yield (serialize (Framed [] :: Framed Message))
61+
(Just m', Nothing) -> yield (serialize m')
62+
(Just m', Just n') -> do
63+
yield ("" <> serialize size <> "" <> m'')
64+
leftover n'
65+
serializeMessage
66+
where m'' = serialize m'
67+
size = B.length m''
68+
toFlushBuilder = do
69+
b <- await
70+
case b of
71+
Just b' -> yield $ Chunk $ Builder.fromByteString b'
72+
Nothing -> yield $ Flush
4873
--------------------------------------------------------------------------------
4974
-- | Wrapped as a HTTP app
5075
httpApp :: ResponseHeaders -> (Request -> IO Message) -> Wai.Application
@@ -53,8 +78,7 @@ httpApp headerFields runConnection' httpRequest = liftIO $ do
5378
let origin = lookupOrigin httpRequest
5479
let headerFields' = insertOrigin headerFields origin
5580

56-
reqs <- parseHTTPRequest httpRequest
57-
mapM runConnection' reqs >>= waiResponse headerFields'
81+
return $ Wai.responseSource status200 headerFields' (banana httpRequest runConnection')
5882

5983
where lookupOrigin req = case lookup "Origin" (Wai.requestHeaders req) of
6084
Just origin -> origin

0 commit comments

Comments
 (0)