将命令行封装成WebSocket可以访问的工具:websocketd

jopen 11年前

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.

项目主页:http://www.open-open.com/lib/view/home/1402819990403