-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.scm
78 lines (63 loc) · 2.38 KB
/
client.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(load "socketio.scm")
(js-load "/socket.io/socket.io.js" "io")
(define (log-debug . args)
(apply js-invoke (append (list (js-eval "console") "log") args)))
(define *io* (js-eval "io"))
(define *socket* (js-invoke *io* "connect"))
(define (handle . args)
(apply socketio-on (cons *socket* args)))
(define (send . args)
(apply socketio-emit (cons *socket* args)))
(define (clear!)
(element-empty! "#message")
(element-focus! "#message"))
(define (add-message! from msg)
(element-append-child! "#lines" (element-new (list 'p (list 'b from) msg))))
(handle "connect"
(lambda ()
(element-add-class-name "#chat" "connected")
(element-focus! "#nick")))
(handle "announcement"
(lambda (msg)
(element-append-child! "#lines" (element-new (list 'p (list 'em msg))))))
(handle "nicknames"
(lambda (nicknames)
(element-empty! "#nicknames")
(element-append-child! "#nicknames" (element-new '(span "Online: ")))
(for-each
(lambda (n) (element-append-child! "#nicknames" (element-new (list 'b n))))
(list-sort nicknames))))
(handle "user message" add-message!)
(handle "reconnect"
(lambda ()
(element-empty! "#lines")
(add-message! "System" "Reconnected to the server")))
(handle "reconnecting"
(lambda ()
(add-message! "System" "Attempting to reconnect to the server")))
(handle "error"
(lambda (err)
(if err
(add-message! "System" err)
(add-message! "System" "An unknown error occurred"))))
(add-handler! "#set-nickname"
"submit"
(lambda ()
(send "nickname"
(element-content "#nick")
(lambda (taken)
(if taken
(element-show! "#nickname-err")
(begin
(element-hide! "#nickname-err")
(clear!)
(element-add-class-name "#chat" "nickname-set")))))
#f))
(add-handler! "#send-message"
"submit"
(lambda ()
(add-message! "me" (element-content "#message"))
(send "user message" (element-content "#message"))
(clear!)
(js-invoke ($ "#lines") "scrollTop" 10000000)
#f))