Created
March 12, 2012 14:11
-
-
Save DerGuteMoritz/2022161 to your computer and use it in GitHub Desktop.
Channel based chat example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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