March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -1,37 +1,21 @@
|
|||
#lang racket
|
||||
|
||||
(define *client-outputs* (list (current-output-port)))
|
||||
|
||||
(define ((tell-all who current-out) line)
|
||||
(for ([out *client-outputs*] #:unless (eq? current-out out))
|
||||
(displayln (~a who ": " line) out)))
|
||||
|
||||
(define (serve-client input output)
|
||||
(define nick (begin
|
||||
(display "Nick: " output)
|
||||
(read-line input)))
|
||||
(define tell (tell-all nick output))
|
||||
(define outs (list (current-output-port)))
|
||||
(define ((tell-all who o) line)
|
||||
(for ([c outs] #:unless (eq? o c)) (displayln (~a who ": " line) c)))
|
||||
|
||||
(define ((client i o))
|
||||
(define nick (begin (display "Nick: " o) (read-line i)))
|
||||
(define tell (tell-all nick o))
|
||||
(let loop ([line "(joined)"])
|
||||
(cond [(eof-object? line)
|
||||
(tell "(left)")
|
||||
(set! *client-outputs* (remq output *client-outputs*))
|
||||
(close-output-port output)]
|
||||
[else
|
||||
(tell line)
|
||||
(loop (read-line input))])))
|
||||
(if (eof-object? line)
|
||||
(begin (tell "(left)") (set! outs (remq o outs)) (close-output-port o))
|
||||
(begin (tell line) (loop (read-line i))))))
|
||||
|
||||
(define (chat-server listener)
|
||||
(define-values [client-input client-output] (tcp-accept listener))
|
||||
(file-stream-buffer-mode client-input 'none)
|
||||
(file-stream-buffer-mode client-output 'none)
|
||||
(thread (λ ()
|
||||
(serve-client client-input client-output)))
|
||||
(set! *client-outputs* (cons client-output *client-outputs*))
|
||||
(chat-server listener))
|
||||
(define-values [i o] (tcp-accept listener))
|
||||
(for ([p (list i o)]) (file-stream-buffer-mode p 'none))
|
||||
(thread (client i o)) (set! outs (cons o outs)) (chat-server listener))
|
||||
|
||||
(define (start-server)
|
||||
(chat-server (tcp-listen 12321)))
|
||||
|
||||
(void (thread start-server))
|
||||
(client (current-input-port) (current-output-port))
|
||||
(void (thread (λ() (chat-server (tcp-listen 12321)))))
|
||||
((client (current-input-port) (current-output-port)))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue