[racket] web-server and comet-like requests?

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Tue Feb 22 14:00:18 EST 2011

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.

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?


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))


Posted on the users mailing list.