eschulte pushed a commit to branch master in repository elpa. commit c04c1f6265eb949db18473c4240e7dd3358fcfa9 Author: Eric Schulte <schulte.e...@gmail.com> Date: Tue Feb 25 22:56:17 2014 -0700
dangerous example; web-socket comint shell buffer --- examples/018-web-shell.el | 61 +++++++++++++++++++++++++++++++++++++++++++ examples/018-web-shell.html | 15 ++++++++++ examples/018-web-shell.js | 22 +++++++++++++++ 3 files changed, 98 insertions(+), 0 deletions(-) diff --git a/examples/018-web-shell.el b/examples/018-web-shell.el new file mode 100644 index 0000000..78814d1 --- /dev/null +++ b/examples/018-web-shell.el @@ -0,0 +1,61 @@ +;;; web-shell.el --- interact with a SHELL through a web interface + +;; Copyright (C) 2013 Eric Schulte <schulte.e...@gmail.com> + +;;; Commentary: + +;; DO NOT RUN THIS EXAMPLE! + +;; At least not if anyone has network access to your computer. + +;; This example starts a local shell using the `shell' function. The +;; resulting comint buffer is then exported using web sockets. +;; Clients can run local shell commands and see their results through +;; their browser. + +;; This example is included because it should be easily generalizable +;; to build web interfaces to other comint buffers using web sockets. + +;;; Code: +(defvar web-shell-port 9018) + +(defun web-shell-f-to-s (f) + (with-temp-buffer + (insert-file-contents-literally + (expand-file-name f + (file-name-directory + (or load-file-name buffer-file-name default-directory)))) + (buffer-string))) + +(defvar web-shell-js (web-shell-f-to-s "018-web-shell.js")) + +(defvar web-shell-html (web-shell-f-to-s "018-web-shell.html")) + +(defvar web-shell-socket nil) + +(defun web-shell-socket-respond (string) + (when web-shell-socket + (process-send-string web-shell-socket (ws-web-socket-frame string)))) + +(defun web-shell-socket-handler (process string) + (message "recieved %S" string) + (with-current-buffer "*shell*" + (goto-char (process-mark (get-buffer-process (current-buffer)))) + (insert string) + (comint-send-input))) + +(defun web-shell-handler (request) + (with-slots (process headers) request + ;; if a web-socket request + (if (ws-web-socket-connect request 'web-shell-socket-handler) + ;; then connect and keep open + (prog1 :keep-alive + (setq web-shell-socket process) + (add-hook 'comint-output-filter-functions 'web-shell-socket-respond)) + ;; otherwise send the html and javascript + (save-window-excursion (shell)) + (ws-response-header process 200 '("Content-type" . "text/html")) + (process-send-string process + (format web-shell-html (format web-shell-js web-shell-port)))))) + +(ws-start 'web-shell-handler 9018) diff --git a/examples/018-web-shell.html b/examples/018-web-shell.html new file mode 100644 index 0000000..56d5ae5 --- /dev/null +++ b/examples/018-web-shell.html @@ -0,0 +1,15 @@ +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> + <head> + <script type="text/javascript"> +%s + </script> + </head> +<body> + <pre id="buffer"></pre> + <div> + <input type="text" id="mini-buffer" value="" /> + <button onclick="connect();">connect</button> + <button onclick="ws.close();">close</button> + </div> +</body> +</html> diff --git a/examples/018-web-shell.js b/examples/018-web-shell.js new file mode 100644 index 0000000..295ff7c --- /dev/null +++ b/examples/018-web-shell.js @@ -0,0 +1,22 @@ +var ws; + +function write(data){ + var before = document.getElementById("buffer").innerHTML; + document.getElementById("buffer").innerHTML = before + data; + window.scrollTo(0,document.body.scrollHeight); } + +function read(){ + var tmp = document.getElementById("mini-buffer").value; + document.getElementById("mini-buffer").value = ""; + write(tmp + "\n"); + return tmp; } + +function connect(){ + ws = new WebSocket("ws://localhost:%d/"); + ws.onopen = function() { write("<p><i>connected</i></p>"); }; + ws.onmessage = function(msg) { write(msg.data); }; + ws.onclose = function() { write("<p><i>closed</i></p>"); }; } + +window.onload = function(){ + document.getElementById("mini-buffer").addEventListener( + "keyup", function(e){ if(e.keyCode == 13){ ws.send(read()); } }); }