Welcome to the CHICKEN Scheme pasting service

no title added by buhman on Sun Dec 2 21:35:29 2018

(use tcp6
     srfi-18
     matchable)

(define (chat-message text port)
  (let ((msg (list 'event 'chat-message text)))
    (write msg port)))

(define (input-loop out)
  (let loop ()
    (print "input-loop-begin")
    (let ((line (read-line)))
      (when (not (eq? line #!eof))
        (chat-message line out)
        (thread-yield!)
        (print "input-loop-end")
        (loop))))
  (print "input-loop-terminate"))

(define +hostname+ "localhost")
(define +port+ 4242)

(define (dispatch-message msg)
  (match msg
    (`(event . ,evt)
     (print "evt:" evt)
     (flush-output))))

(define (handle-message in)
  (let ((msg (read in)))
    (case msg
      ((#!eof) #f)
      (else
       (dispatch-message msg)
       #t))))

(define (client-loop in)
  (let handle-next-message ()
    (print "client-loop-begin")
    (when (and (not (port-closed? in))
               (handle-message in))
      (thread-yield!)
      (print "client-loop-end")
      (handle-next-message)))
  (print "client-loop-terminate"))

(define (start-client)
  (let-values (((in out) (tcp-connect +hostname+ +port+)))
    (let ((client (thread-start! (lambda () (client-loop in))))
          (input (thread-start! (lambda () (input-loop out)))))
      (thread-join! client)
      (thread-join! input))))

(start-client)

;; usage:

> csi -s chat-client.scm
client-loop-begin
input-loop-begin
asdf
input-loop-end
input-loop-begin
1234
input-loop-end
input-loop-begin
input-loop-terminate
evt:(chat-message (asdf))
client-loop-end
client-loop-begin
evt:(chat-message (1234))
client-loop-end
client-loop-begin

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
Proper ... recursion is required by the Scheme specification.
Visually impaired? Let me spell it for you (wav file) download WAV