why use random numbers for client ids at all
[8sync.git] / 8sync / systems / websocket / server.scm
index 04e961c25b2997feecb65b32eae0024f2631b674..97645bb8ef3490a0b5b6f0e06c4b235b1e252925 100644 (file)
@@ -59,6 +59,12 @@ string."
 
 (define no-op (const #f))
 
+(define (make-simple-counter)
+  (let ((count 0))
+    (lambda ()
+      (set! count (1+ count))
+      count)))
+
 (define-actor <websocket-server> (<web-server>)
   ((ws-send websocket-server-send))
   (upgrade-paths #:init-value `(("websocket" .
@@ -66,6 +72,8 @@ string."
                  #:allocation #:each-subclass
                  #:accessor .upgrade-paths)
 
+  (gen-client-id #:init-thunk make-simple-counter)
+
   ;; active websocket connections
   (ws-clients #:init-thunk make-hash-table
               #:accessor .ws-clients)
@@ -79,6 +87,9 @@ string."
                            #:init-value no-op
                            #:getter .on-ws-client-disconnect))
 
+(define (web-server-gen-client-id websocket-server)
+  ((slot-ref websocket-server 'gen-client-id)))
+
 (define (websocket-client-loop websocket-server client request body)
   "Serve client connected via CLIENT by performing the HTTP
 handshake and listening for control and data frames.  HANDLER is
@@ -97,7 +108,7 @@ called for each complete message that is received."
 
   ;; Allows other actors to send things to this specific client
   ;; @@: We probably could just increment a counter...
-  (define client-id (big-random-number))
+  (define client-id (web-server-gen-client-id websocket-server))
 
   (define (close-down)
     (close-port client)