Skip to content

Commit

Permalink
Sending ping event when starting long polling
Browse files Browse the repository at this point in the history
  • Loading branch information
biilmann committed Sep 27, 2011
1 parent e761d68 commit 9908de9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/EventStream.hs
Expand Up @@ -103,6 +103,11 @@ field l b = l `mappend` b `mappend` nl
-}
flushAfter b = b `mappend` flush

{-|
Send a comment with the string "ping" to the client.
-}
pingEvent = flushAfter $ field commentField (fromString "ping")


{-|
Converts a 'ServerEvent' to its wire representation as specified by the
Expand All @@ -123,19 +128,18 @@ eventSourceBuilder (ServerEvent n i d)= Just $ flushAfter $

eventSourceEnum source builder timeoutAction finalizer = withInitialPing
where
withInitialPing (Continue k) = k (Chunks [ping]) >>== go
withInitialPing (Continue k) = k (Chunks [pingEvent]) >>== go
go (Continue k) = do
liftIO $ timeoutAction 10
event <- liftIO $ timeout 9000000 source
case fmap builder event of
Just (Just b) -> k (Chunks [b]) >>== go
Just Nothing -> k EOF
Nothing -> do
k (Chunks [ping]) >>== go
k (Chunks [pingEvent]) >>== go
go step = do
liftIO finalizer
returnI step
ping = flushAfter $ field commentField (fromString "ping")


{-|
Expand All @@ -155,6 +159,7 @@ eventStream source builder finalizer = do
-}
eventResponse :: IO ServerEvent -> (ServerEvent -> Maybe Builder) -> IO () -> Snap ()
eventResponse source builder finalizer = do
writeBuilder pingEvent
event <- liftIO $ source `onException` finalizer
case builder event of
Just b -> writeBuilder b
Expand Down
16 changes: 10 additions & 6 deletions src/Main.hs
Expand Up @@ -77,9 +77,8 @@ brokerInfo :: DB -> UString -> Snap ()
brokerInfo db uuid = do
result <- liftIO $ Conn.count db uuid
case result of
Right count -> do
modifyResponse $ setContentType "application/json"
writeBS $ BS.pack $ "{\"brokerId\": " ++ (show uuid) ++ ", \"connections\": " ++ (show count) ++ "}"
Right count ->
sendJSON $ BS.pack $ "{\"brokerId\": " ++ (show uuid) ++ ", \"connections\": " ++ (show count) ++ "}"
Left e -> do
modifyResponse $ setResponseCode 500
writeBS $ BS.pack $ "Database Connection Problem: " ++ (show e)
Expand All @@ -103,9 +102,8 @@ createSocket db uuid = do
Left failure -> do
logError (BS.pack $ show failure)
showError 500 "Database Connection Error"
Right _ -> do
modifyResponse $ setContentType "application/json"
writeBS $ BS.pack ("{\"socket\": \"" ++ socketId ++ "\"}")
Right _ ->
sendJSON $ BS.pack ("{\"socket\": \"" ++ socketId ++ "\"}")


postEvent :: DB -> Channel -> UString -> Snap ()
Expand Down Expand Up @@ -199,6 +197,12 @@ showError code msg = do
finishWith r


sendJSON :: ByteString -> Snap ()
sendJSON json = do
modifyResponse $ setContentType "application/json"
writeBS json


-- |Returns the transport method to use for this request
getTransport :: Snap (IO ServerEvent -> IO () -> Snap ())
getTransport = withRequest $ \request ->
Expand Down
5 changes: 3 additions & 2 deletions static/eventsource.polyfill.js
Expand Up @@ -29,6 +29,7 @@
if ("EventSource" in window) return;

var reTrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g;
var onProgressSupport = navigator.userAgent.match(/Firefox/);

var EventSource = function (url) {
var eventsource = this,
Expand Down Expand Up @@ -62,7 +63,7 @@ var EventSource = function (url) {

// we must make use of this on the server side unlesss we're using firefox - because they don't trigger
// readychange until the server connection is closed
if (!navigator.userAgent.match(/Firefox/)) {
if (!onProgressSupport) {
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
}

Expand All @@ -71,7 +72,7 @@ var EventSource = function (url) {

xhr.timeout = 50000;
xhr.onreadystatechange = function () {
if ((this.readyState == 3 || this.readyState == 4) && this.status == 200) {
if (((onProgressSupport && this.readyState == 3) || this.readyState == 4) && this.status == 200) {
// on success
if (eventsource.readyState == eventsource.CONNECTING) {
eventsource.readyState = eventsource.OPEN;
Expand Down
6 changes: 6 additions & 0 deletions templates/eshq.js.st
@@ -1,6 +1,12 @@
(function() {
var origin = "$origin$";

if (typeof(window.addEventListener) == "undefined") {
window.addEventListener = function(name, fn) {
window.attachEvent("on" + name, fn);
};
}

var post = function(path, data, callback) {
var xhr = new XMLHttpRequest();
xhr.open('POST', path, true);
Expand Down

0 comments on commit 9908de9

Please sign in to comment.