Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Turn any program that uses STDIN/STDOUT into a WebSocket server. Like inetd, but for WebSockets.

License

NotificationsYou must be signed in to change notification settings

joewalnes/websocketd

Repository files navigation

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 readsSTDIN and writes toSTDOUT, you can build a WebSocket server. Do it in Python, Ruby, Perl, Bash, .NET, C, Go, PHP, Java, Clojure, Scala, Groovy, Expect, Awk, VBScript, 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'sSTDIN stream, followed by a\n newline.

Any text printed by the process toSTDOUT shall be sent as a WebSocket message whenever a\n newline is encountered.

Download

If you're on a Mac, you can installwebsocketd usingHomebrew. Just runbrew install websocketd. For other operating systems, or if you don't want to use Homebrew, check out the link below.

Download for Linux, OS X 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/bashfor((COUNT=1; COUNT<=10; COUNT++));doecho$COUNT  sleep 1done

Before turning it into a WebSocket server, let's test it from the command line. The beauty ofwebsocketd 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.sh12345678910

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><preid="log"></pre><script>// helper function: log message to screenfunctionlog(msg){document.getElementById('log').textContent+=msg+'\n';}// setup websocket with callbacksvarws=newWebSocket('ws://localhost:8080/');ws.onopen=function(){log('CONNECT');};ws.onclose=function(){log('DISCONNECT');};ws.onmessage=function(event){log('MESSAGE: '+event.data);};</script>

Open this page in your web-browser. It will even work if you open it directlyfrom disk using afile:// URL.

More Features

  • Very simple install. Justdownload 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 standardCGI 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 viawebsocketd --help.
  • IncludesWebSocket 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.

User Manual

More documentation in the user manual

Example Projects

Got more examples? Open a pull request.

My Other Projects

Andfollow @joewalnes!

About

Turn any program that uses STDIN/STDOUT into a WebSocket server. Like inetd, but for WebSockets.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp