需先给脚本 chmod +x 并确保其可独立在命令行运行;--passstderr 时 STDERR 以 JSON 标记与 STDOUT 混发;脚本派生的子进程会随连接断开被整组终止,需存活时用 setsid
websocketd
joewalnesBSD-2-Clause活跃维护把任何读写标准输入输出的命令行程序直接变成 WebSocket 服务器,像 inetd 一样按连接启动进程,省去自己写 WebSocket 服务端。
Turn any program that uses STDIN/STDOUT into a WebSocket server. Like inetd, but for WebSockets.
brew install websocketd
websocketd --port=8080 ./count.shREADME 未说明
把任何读写 STDIN/STDOUT 的程序包装成 WebSocket 服务器,无需网络库
把任何读写标准输入输出的命令行程序直接变成 WebSocket 服务器,像 inetd 一样按连接启动进程,省去自己写 WebSocket 服务端。
核心能力
- 连接时 fork 进程,WebSocket 断开即终止进程
- 客户端消息按行写入进程 STDIN,STDOUT 按行回推
- 通过 CGI 环境变量向脚本传递请求信息
- 内置静态文件服务器和经典 CGI 服务器
- 提供 --devconsole 网页调试控制台收发帧
容易踩的坑
- v0.4.1January 24, 2021最新正式版2021-01-25 · 2021-01-25 02:04 UTC
This is hopefully a full release to replace pre-released 0.3.1 that did not go anywhere.
No signed CHECKSUMS until I finalize few checks.
- v0.3.1Version 0.3.1pre-release2019-01-28 · 2019-01-28 13:36 UTC
查看更新说明
Go was significantly upgraded since 1.9 so it's time to re-build. Besides that websocketd did not change. Development-wise the build is now utilizes go modules and fixed to use 1.4.0 gorilla-websockets.
This is pre-release so there is no signed checksums file yet.
- v0.3.0Version 0.3.0pre-release2017-12-14 · 2017-12-14 02:55 UTC
查看更新说明
This release includes migration of websocketd to use gorilla websocket library and:
- fixes PING/PONG behavior (to the best of our knowledge)
- improves websocketd behavior in Windows
- rebuilds OSX binary to be suitable for High Sierra
Since the migration to new library is always a risk it's recommended to be careful and experiment with this version before using it for something important.
PS. CHECKSUMS clearsigned with key 325F6BA1 using gpg.
- v0.2.12Version 0.2.122016-02-17 · 2016-02-17 22:54 UTC
查看更新说明
Changes from 0.2.11:
- Commands without text IO now could use binary websocket messages to communicate
- Code compiled with go 1.6 which drops support for SSL3 and fixes other less than ideal SSL protocol features
- Minor changes were applied to examples and --help output
CHECKSUMS signed with key 325F6BA1. You can verify signature with gpg.
- v0.2.11Version 0.2.112015-07-02 · 2015-07-02 14:43 UTC
查看更新说明
Changes from 0.2.10:
- PATH env variable is now passed to process by default
- new --header* flags could generate custom HTTP headers for all websocketd-generated answers
- there is no more bug causing process to hang when WebSockets client disconnect is detected
- there were also minor changes for console app (default url building logic and tab char printing)
Code also has multiple changes of examples.
Binaries below have CHECKSUMS.asc file where content is signed by key 325F6BA1 that's published on keyserver.pgp.com.
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
websocketd
websocketd is a small command-line tool that will wrap an existing command-line interface program, and allow it to be accessed via a WebSocket.
WebSocket-capable applications can now be built very easily. As long as you can write an executable program that reads STDIN and writes to STDOUT, you can build a WebSocket server. Do it in Python, Ruby, Perl, Bash, .NET, C, Go, PHP, Java, Clojure, Scala, Groovy, Expect, Awk, VBScript, PowerShell, Haskell, Lua, R, whatever! No networking libraries necessary.
Details
Upon startup, websocketd will start a WebSocket server on a specified port, and listen for connections.
Upon a connection, it will fork the appropriate process, and disconnect the process when the WebSocket connection closes (and vice-versa).
Any message sent from the WebSocket client will be piped to the process's STDIN stream, followed by a \n newline.
Any text printed by the process to STDOUT shall be sent as a WebSocket message whenever a \n newline is encountered.
Download
If you're on a Mac, you can install websocketd using Homebrew. Just run brew install websocketd. For other operating systems, or if you don't want to use Homebrew, check out the link below.
Download for Linux, macOS and Windows
Quickstart
To get started, we'll create a WebSocket endpoint that will accept connections, then send back messages, counting to 10 with 1 second pause between each one, before disconnecting.
To show how simple it is, let's do it in Bash!
count.sh:
#!/bin/bash
for ((COUNT = 1; COUNT <= 10; COUNT++)); do
echo $COUNT
sleep 1
done
Before turning it into a WebSocket server, let's test it from the command line. The beauty of websocketd is that servers work equally well in the command line, or in shell scripts, as they do in the server - with no modifications required.
$ chmod +x count.sh
$ ./count.sh
1
2
3
4
5
6
7
8
9
10
Now let's turn it into a WebSocket server:
$ websocketd --port=8080 ./count.sh
Finally, let's create a web-page to test it.
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
const ws = new WebSocket('ws://localhost:8080/');
ws.onopen = () => {
log('CONNECT');
};
ws.onclose = () => {
log('DISCONNECT');
};
ws.onmessage = (event) => {
log(`MESSAGE: ${event.data}`);
};
</script>
Open this page in your web-browser. It will even work if you open it directly
from disk using a file:// URL.
More Features
-
Very simple install. Just download the single executable for Linux, Mac or Windows and run it. Minimal 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. Note that
SERVER_NAMEandSERVER_PORTmirror the request'sHostheader (virtual-host semantics, as innet/http/cgi), so a client that controls itsHostheader controls them too. Do not make access decisions on them. -
As well as serving websocket daemons it also includes a static file server and classic CGI server for convenience.
-
Can listen on a Unix domain socket (
--unixsocket) instead of, or alongside, a TCP address. That exposes websocketd only to processes on the same host, e.g. behind an SSH-forwarded or reverse-proxied socket.--socketmode(e.g.0700) pins the socket's permissions instead of leaving them to the process umask. -
STDERR can optionally be forwarded to WebSocket clients (
--passstderr), tagged alongside STDOUT as JSON so a client can tell the two apart. -
When a client disconnects, the command is shut down, and so is any child process it spawned. Teardown signals the whole process group. Scripts that deliberately spawn survivors should start them in their own session (
setsid). -
Command line help available via
websocketd --help. -
Includes a built-in WebSocket developer console (
--devconsole) to test and debug your scripts before you've built a JavaScript frontend. Connect, send a frame, and read back what your script wrote to stdout, with no client code. A compact frame list sits beside a detail pane for the selected frame's opcode, size, and pretty/raw/hex payload views, and it works in light or dark.Watch a short demo of connect → send → receive.
-
Examples in many programming languages are available to help you getting started.
Documentation
Example Projects
- Plot real time Linux CPU/IO/Mem stats to a HTML5 dashboard using websocketd and vmstat (for Linux)
- Arbitrary REPL in the browser using websocketd
- Retrieve SQL data from server with LiveCode and webSocketd
- List files from a configured folder (for Linux)
- Listen for gamepad events and report them to the system (this + android = gamepad emulator)
Got more examples? Open a pull request.
My Other Projects
- ReconnectingWebSocket - Simplest way to add some robustness to your WebSocket connections.
- Smoothie Charts - JavaScript charts for streaming data.
And follow @joewalnes!