將命令行封裝成WebSocket可以訪問的工具:websocketd
websocketd是一個小的命令行工具,將已有的命令行接口程序進行包裝,并允許通過WebSocket訪問它。
例如這樣一個命令行程序 count.sh:
#!/bin/bash for COUNT in $(seq 1 10); do echo $COUNT sleep 1 done
可通過如下命令進行封裝:
$ websocketd --port=8080 ./count.sh
然后你可以創建一個網頁來測試 count.html:
<!DOCTYPE html> <pre id="log"></pre> <script> // helper function: log message to screen function log(msg) { document.getElementById('log').textContent += msg + '\n'; } // setup websocket with callbacks var ws = new WebSocket('ws://localhost:8080/'); ws.onopen = function() { log('CONNECT'); }; ws.onclose = function() { log('DISCONNECT'); }; ws.onmessage = function(event) { log('MESSAGE: ' + event.data); }; </script>
特性
- Very simple install. Just download the single executable for Linux, Mac or Windows and run it. No dependencies, no installers, no package managers, no external libraries. Suitable for development and production servers.
- Server side scripts can access details about the WebSocket HTTP request (e.g. remote host, query parameters, cookies, path, etc) via standard CGI environment variables.
- As well as serving websocket daemons it also includes a static file server and classic CGI server for convenience.
- Command line help available via
websocketd --help
. - Includes WebSocket developer console to make it easy to test your scripts before you've built a JavaScript frontend.
- Examples in many programming languages are available to help you getting started.
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!