[racket] web-server and comet-like requests?
2011/2/22 Danny Yoo <dyoo at cs.wpi.edu>:
> On Tue, Feb 22, 2011 at 12:51 PM, Jay McCarthy <jay.mccarthy at gmail.com> wrote:
>> The Web Server will kill connections after a relatively short timeout.
>> You'll get an exception when you finally try to use the connection.
>> This isn't very nice for COMET. You'll have to reset the timeout on
>> the connection manually.
>
>
> Two comments:
>
> 1. Is output-response from web-server/http/response documented? I
> searched and couldn't find it. It seems a critical function for
> writing dispatchers.
It is not documented. I imagined people would just throw
next-dispatcher and drop to a lifted procedure (dispatch-lift) or
their servlet.
>
> 2. If a user cancels an http request (say, by pressing the stop
> button on the browser), is that observable from the web-server side of
> things?
You can't tell the difference between that and the connection closing
for any other reason.
Jay
>
>
> I have something like this now, but unfortunately, it's not handling
> the scenario from question 2 correctly:
>
> ;;;;;;;;;;;;;;;;;;;
> #lang racket
>
> ;; Comet demonstration
>
> (require web-server/web-server
> web-server/http/bindings
> web-server/http/response
> web-server/http/response-structs)
>
> (define ch (make-channel))
>
> (void
> (thread (lambda ()
>
> (define (my-dispatcher conn req)
> (cond [(exists-binding? 'comet (request-bindings req))
> (handle-comet conn req)]
> [else
> (handle-default conn req)]))
> (serve #:dispatch my-dispatcher #:port 8080))))
>
>
> (define (handle-comet conn req)
> (let ([v (sync ch)])
> (with-handlers ([exn:fail? (lambda (exn)
> (printf "exn: ~s\n" exn)
> (thread (lambda () (channel-put ch v))))])
> (output-response conn
> (response/full 200 #"Okay"
> (current-seconds)
> #"text/plain; charset=utf-8"
> empty
> (list #"" (string->bytes/utf-8
> (format "~s" v))))))))
>
> (define (handle-default conn req)
> (output-response conn '(html (body (p "hello world")))))
>
> (define (send-to-client v)
> (channel-put ch v))
>
--
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay
"The glory of God is Intelligence" - D&C 93