Skip to content

Instantly share code, notes, and snippets.

@DerGuteMoritz
Created March 12, 2012 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DerGuteMoritz/2022161 to your computer and use it in GitHub Desktop.
Save DerGuteMoritz/2022161 to your computer and use it in GitHub Desktop.
Channel based chat example
(use unix-sockets channel srfi-18 parley)
(define output-channel
(make-channel))
(channel-enqueue output-channel (parley "Your name: "))
(define-values (in out)
(unix-connect "sock"))
(flush-channel-to-output-port
output-channel out write-line)
(thread-start!
(lambda ()
(let loop ()
(let ((line (parley "")))
(unless (eof-object? line)
(channel-enqueue output-channel line)
(loop))))))
(define-values (next channel)
(siphon-input-port in read-line))
(on-channel-receive channel print)
(let loop ()
(when (next)
(loop)))
(use unix-sockets channel srfi-18)
(define socket-file "sock")
(when (file-exists? socket-file)
(delete-file socket-file))
(define listener
(unix-listen socket-file))
(define broadcast-channel
(make-channel))
(on-channel-receive broadcast-channel print)
(let loop ()
(define-values (in out) (unix-accept listener))
(thread-start!
(lambda ()
(define name (read-line in))
(print name " joined")
(flush-channel-to-output-port
broadcast-channel out write-line)
(define-values (next channel)
(siphon-input-port in read-line))
(siphon-channel
(map-channel
channel
(lambda (message)
(string-append name ": " message)))
broadcast-channel)
(let loop ()
(when (next)
(loop)))))
(loop))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment