The order of arguments you pass to long-poll-newest doesn't look
right.

You defined long-poll-newest to optionally take a fourth parameter for
the existing xhr connection. However, when calling long-poll-newest
you pass the existing xhr connection as the first argument.

Replace
(long-poll-newest xhr1 url callback error-callback)
with
(long-poll-newest url callback error-callback xhr1)

Also, all of the (do ..)'s are unnecessary. 'let', 'fn' and 'when'
evaluate the body in an implicit 'do' themselves.

The overall approach to reuse the connection looks fine otherwise.

-Gijs

On Apr 30, 12:00 pm, Dusan <dusan.milorado...@gmail.com> wrote:
> I am trying to figure out how to setup the long polling from the
> clojurescript.
>
> I use aleph on the server side. Here is the trivial aleph handler and
> related code:
>
> (def k (permanent-channel))
>
> (receive-all k (fn[x] (println "got " x)))
>
> (defn longpoll-new [ch request]
>   (siphon k ch)
>   )
>
> (defn send-mes [k mes]
>   (enqueue k ((comp r/response pr-str) mes)))
>
> On the clojurescript side I am trying to open one xhr connection, and use
> it for all subsequent calls:
>
> (defn long-poll-newest
>   ([url callback error-callback]
>      (long-poll-newest  url callback error-callback (net/xhr-connection)))
>   ([url callback error-callback xhr1]
>      (do
>        (event/listen xhr1
>                      :success
>                      (fn[e]
>                        (do
>                          (callback (. xhr1 (getResponseText)))
>                          (long-poll-newest xhr1 url callback error-callback)
>                          )))
>        (event/listen xhr1
>                      :error
>                      (fn[e]
>                        (when error-callback
>                          (do
>                            (println "entered the error callback")
>                            (error-callback e)
>                            )
>                          )))
>        (event/listen xhr1
>                      :timeout
>                      (fn[e]
>                        (do
>                          (long-poll-newest xhr1 url callback error-callback)
>                          )
>                        ))
>        (net/transmit xhr1 url))
>      ))
>
> Unfiortunatelly, this receives just the first message from server, and then
> it stops.
>
> This is the version that is working:
> (defn long-poll [url callback error-callback]
>   (let [xhr1 (net/xhr-connection)]
>     (do
>       (event/listen xhr1
>                     :success
>                     (fn[e]
>                       (do
>                         (callback (. xhr1 (getResponseText)))
>                         (long-poll url callback error-callback)
>                         )))
>       (event/listen xhr1
>                     :error
>                     (fn[e]
>                       (when error-callback
>                         (do
>                           (println "entered the error callback")
>                           (error-callback e)
>                           )
>                         )))
>       (event/listen xhr1
>                     :timeout
>                     (fn[e]
>                       (do
>                         (long-poll url callback error-callback)
>                         )
>                       ))
>       (net/transmit xhr1 url))))
>
> In this version I am closing the original xhr, I am creating the new one
> for each request.
> How can I make the first version functional, what do I miss?
>
> Dusan

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to