https://github.com/ceramic/remote-js.git
git clone 'https://github.com/ceramic/remote-js.git'
(ql:quickload :remote-js)
Send JavaScript from Common Lisp to a browser.
First, we create a context object:
(defvar ctx (remote-js:make-context))
Then we start the WebSockets server:
(remote-js:start ctx)
Now, remote-js gives us a function that generates the HTML of a simple page that
connects to this context and notifies it when it's connected. We write the HTML
to ~/test.html
:
(with-open-file (stream (merge-pathnames #p"test.html" (user-homedir-pathname))
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(write-string (remote-js:html ctx) stream))
Open the file in your browser. Now you can do:
(remote-js:eval ctx "alert('hello!')")
And you will see the alert box pop up in your browser.
remote-js defines a function in the generated HTML, RemoteJS.send
, which takes
a string and sends it to the server. You can specify a callback for receiving
messages like this:
(defvar ctx (remote-js:make-context
:callback #'(lambda (message) (format t "Received: ~A~%" message))))
Then, start everything and generate the HTML file again:
(remote-js:start ctx)
(with-open-file (stream (merge-pathnames #p"test.html" (user-homedir-pathname))
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(write-string (remote-js:html ctx) stream))
And open test.html
in your browser.
Now you can send messages to the server like this:
CL-USER> (remote-js:eval ctx "RemoteJS.send('hi!')")
Received: hi!
Note: when a client connects to the server, it sends the string
remote-js:+connected-message+
.
The tests use trivial-open-browser, and running them will open your default browser to a temporary file.
Copyright (c) 2016 Fernando Borretti
Licensed under the MIT License.