-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.lisp
61 lines (47 loc) · 2.08 KB
/
main.lisp
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
(in-package #:plot-window)
;;;; Starting our Servers
(defun cl-user::initialize-application (&key (port 8765))
(start-web-server :port port)
(start-clws-server)
(start-and-register-json-rpc-resource))
;;;; Our one and only web page.
(define-easy-handler (dw-window :uri "/") ()
(with-my-page (s :title "Display Window")
(with-header-fragment (s)
(emit-script-tags-for-javascript-libraries s '(dw)))
(:div :id "ex1"
:style (css-lite:inline-css `((width "600px") (height "350px")))
"Sir! Your humble window awakes your command.")))
;;;; Inbound messages
(define-json-message-handler page-log (message)
(log:info '(websocket page-log) "~S" message))
;;;; Outbound messages
(defvar *last-global-eval* nil
"For debugging, this retains the last javascript source send to the a browser.")
(defun send-global-eval (javascript-text &optional (client *last-websocket-client*))
"Ask the websocket client to run the javascript source code given
For example: (send-global-eval \"alert('hi')\")
Note that this sets *last-global-eval*."
(log:debug '(websocket send global-eval) "~S" javascript-text)
(setf *last-global-eval* javascript-text)
(send-json-message
`((:event . ,(symbol-to-js-string 'global-eval))
(:argument . ,javascript-text))
client))
(defmacro ps-eval-in-client* (&rest parenscript-forms)
"Compiles the given parenscript into Javascript and sends it to *last-websocket-client*.
For example: (ps-eval-in-client* '(alert \"hi\"))"
`(send-global-eval (ps* ,@parenscript-forms)))
(defparameter *catch-eval-in-client-errors* nil)
(defmacro ps-eval-in-client (&body parenscript-forms)
(let ((body `(progn ,@parenscript-forms)))
(when *catch-eval-in-client-errors*
(setf body `(try ,body
(:catch (e)
((@ dw lg)
(interpolate "[Error: ${(@ e message)}]"))
e))))
`(send-global-eval
(ps (setf (@ dw last-result) ,body)))))
(defun add-element (html-element-text)
(ps-eval-in-client* `(chain dw (insert-element ($ ,html-element-text)))))