Skip to content
Create a gist now

Instantly share code, notes, and snippets.

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
Something went wrong with that request. Please try again.