websocketd README

The project's README from github.com/joewalnes/websocketd. As one markdown file: README.md.

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.

-@joewalnes

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

Documentation

More documentation

Example Projects

Got more examples? Open a pull request.

My Other Projects

And follow @joewalnes!

websocketd (joewalnes/websocketd)

Searching Database…

这是什么东西?websocketd 是一个小巧的命令行工具,它能把现有的命令行程序包装成 WebSocket 服务器——就像 inetd,只不过面向 WebSocket。任何从 STDIN 读取、往 STDOUT 写入的脚本,都能变成一个浏览器可以直接访问的实时端点。
它是怎么把我的脚本变成 WebSocket 服务器的?启动时,它会在你指定的端口上开一个 WebSocket 服务器;每当有客户端连进来,它就把你的程序启动一份新进程,把客户端的消息送进该进程的 STDIN,再把程序输出到 STDOUT 的每一行(以换行结尾)作为 WebSocket 消息发回给客户端。连接关闭时,这个进程也会随之结束。
我的服务端程序可以用哪些语言来写?任何能读 STDIN、写 STDOUT 的可执行程序都行——README 里列出了 Bash、Python、Ruby、Perl、PHP、Java、Go、PowerShell、Haskell、Awk 等等。你完全不需要任何网络库,socket 这部分由 websocketd 全权处理。
在写任何前端代码之前,我能先试一下效果吗?可以。先直接在命令行里运行你的脚本——同一个程序在终端里的行为和作为 WebSocket 端点时完全一致。websocketd 还自带一个开发者控制台(--devconsole),不用写一行 JavaScript 就能建立连接、发送数据帧并查看返回结果。
用它我到底能做些什么?README 里的示例项目包括:通过 vmstat 把 Linux 的 CPU/IO/内存实时状态画到 HTML5 仪表盘上、在浏览器里做一个任意 REPL、用 LiveCode 查询 SQL 数据、列出某个文件夹里的文件,还有一个配合 Android 手机的手柄事件监听器,可以把手机变成游戏手柄模拟器。
能不能不让它暴露在公网网卡上?可以——它可以监听 Unix domain socket(--unixsocket),而不是 TCP 地址,或者两者同时使用,并可以用 --socketmode(例如 0700)来收紧权限。这样就只有本机上的进程能访问它,比如放在 SSH 转发或反向代理的 socket 后面。
调试时,我脚本的错误输出能传到浏览器里吗?可以,只需加上 --passstderr:STDERR 会和 STDOUT 一起转发给 WebSocket 客户端,并打上 JSON 标记,这样客户端就能区分这两路输出。
What are similar projects?ttyd, tailcat, websocket, dufs, uvicorn, miller, fq, http-server
Creator
joewalnes
Size
2.1 MB
Stars
17.5K
Age
13y
Commits · 90 Days