Skip to content

Commit af25cc6

Browse files
committed
Switch to fasthttp
Signed-off-by: Joeky <joeky5888@gmail.com>
0 parents  commit af25cc6

121 files changed

Lines changed: 5214 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
websocketd
2+
go-*

.golangci.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
version: "2"
2+
3+
run:
4+
allow-parallel-runners: true
5+
timeout: 5m
6+
7+
linters:
8+
default: none
9+
enable:
10+
- govet
11+
- ineffassign
12+
- unused
13+
- paralleltest
14+
- staticcheck
15+
- gocritic
16+
- errchkjson
17+
- rowserrcheck
18+
- errcheck
19+
- revive
20+
settings:
21+
errcheck:
22+
exclude-functions: # To exclude more Close funcs, use `errcheck -verbose ./...` to get function names
23+
- (*github.com/fasthttp/websocket.Conn).Close
24+
revive:
25+
enable-all-rules: false
26+
severity: error
27+
rules: # From https://github.com/mgechev/revive/blob/master/defaults.toml
28+
- name: atomic
29+
- name: blank-imports
30+
- name: context-as-argument
31+
- name: context-keys-type
32+
- name: dot-imports
33+
- name: empty-block
34+
- name: error-naming
35+
- name: error-return
36+
- name: error-strings
37+
- name: errorf
38+
- name: increment-decrement
39+
- name: indent-error-flow
40+
- name: range
41+
- name: receiver-naming
42+
- name: redefines-builtin-id
43+
- name: superfluous-else
44+
- name: time-naming
45+
- name: unexported-return
46+
- name: unreachable-code
47+
- name: var-declaration
48+
49+
issues:
50+
fix: false

AUTHORS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
websocketd authors
2+
==================
3+
4+
Joe Walnes <joe@walnes.com>
5+
Gareth Jones <gareth.e.jones@gmail.com>
6+
Ajit George <ajit@ajitgeorge.com>
7+
Alex Sergeyev <abc@alexsergeyev.com>

CHANGES

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Version 0.4.1 (Jan 24, 2021)
2+
3+
* Minor changes only
4+
* Updated to Go 1.15.7
5+
6+
Version 0.3.1 (Jan 28, 2019)
7+
8+
* Minor improvements to websocketd itself
9+
* Use of go modules, gorilla websockets set to 1.4.0
10+
* Binaries build code switched to 1.11.5 (improving underlying protocol handlers)
11+
12+
Version 0.3.0 (??, 2017)
13+
14+
* Migration of underlying websocket server to Gorilla Websocket lib.
15+
* Binaries build code switched to 1.9.2
16+
17+
Version 0.2.12 (Feb 17, 2016)
18+
19+
* Update of underlying go standard libraries change how SSL works. SSL3 is no longer supported.
20+
* Support of commands that do not provide text IO (using them as binary websocket frames)
21+
* Minor changes in examples and --help output
22+
23+
Version 0.2.11 (Jul 1, 2015)
24+
25+
* PATH env variable is now passed to process by default
26+
* new --header* flags could generate custom HTTP headers for all websocketd-generated answers
27+
* fixed bug causing process to hang when WebSockets client disconnect is detected
28+
* minor changes for console app (default url building logic and tab char printing)
29+
* multiple changes of examples.
30+
31+
32+
Version 0.2.10 (Feb 16, 2015)
33+
34+
* fixes for null-origin situations (#75, #96)
35+
* better bash examples (#103)
36+
* changelog and checksums for released files (#101, #105)
37+
38+
39+
Version 0.2.9 (May 19, 2014)
40+
41+
* ability to listen multiple IP addresses (#40, #43)
42+
* proper support for TLS (#17)
43+
* resource limits enforcement (a.k.a. maxforks feature, #46)
44+
* passenv option to limit environment variables visible by running commands (#4)
45+
* fix for problem of closing upgraded websocket connection when script is not found (#29)
46+
* websocket origin restrictions via command line option (#20)
47+
* minor update for help flag behavior
48+
* minor fix for devconsole
49+
50+
Version 0.2.8 (Jan 11, 2014)
51+
52+
* ...

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2014, Joe Walnes and the websocketd authors.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
13+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Makefile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2013 Joe Walnes and the websocketd team.
2+
# All rights reserved.
3+
# Use of this source code is governed by a BSD-style
4+
# license that can be found in the LICENSE file.
5+
6+
# Self contained Go build file that will download and install (locally) the correct
7+
# version of Go, and build our programs. Go does not need to be installed on the
8+
# system (and if it already is, it will be ignored).
9+
10+
# To manually invoke the locally installed Go, use ./go
11+
12+
# Go installation config.
13+
GO_VER=1.11.5
14+
SYSTEM_NAME:=$(shell uname -s | tr '[:upper:]' '[:lower:]')
15+
SYSTEM_ARCH:=$(shell uname -m)
16+
GO_ARCH:=$(if $(filter x86_64, $(SYSTEM_ARCH)),amd64,386)
17+
GO_VERSION:=$(GO_VER).$(SYSTEM_NAME)-$(GO_ARCH)
18+
GO_DOWNLOAD_URL:=https://dl.google.com/go/go$(GO_VERSION).tar.gz
19+
GO_DIR:=go-$(GO_VER)
20+
21+
# Build websocketd binary
22+
websocketd: $(GO_DIR)/bin/go $(wildcard *.go) $(wildcard libwebsocketd/*.go)
23+
$(GO_DIR)/bin/go build
24+
25+
localgo: $(GO_DIR)/bin/go
26+
27+
# Download and unpack Go distribution.
28+
$(GO_DIR)/bin/go:
29+
mkdir -p $(GO_DIR)
30+
rm -f $@
31+
@echo Downloading and unpacking Go $(GO_VERSION) to $(GO_DIR)
32+
curl -s $(GO_DOWNLOAD_URL) | tar xfz - --strip-components=1 -C $(GO_DIR)
33+
34+
# Clean up binary
35+
clean:
36+
rm -rf websocketd
37+
38+
.PHONY: clean
39+
40+
# Also clean up downloaded Go
41+
clobber: clean
42+
rm -rf $(wildcard go-v*)
43+
44+
.PHONY: clobber

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# websocketd
2+
3+
`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.
4+
5+
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, Haskell, Lua, R, whatever! No networking libraries necessary.
6+
7+
-[@joewalnes](https://twitter.com/joewalnes)
8+
9+
## Details
10+
11+
Upon startup, `websocketd` will start a WebSocket server on a specified port, and listen for connections.
12+
13+
Upon a connection, it will fork the appropriate process, and disconnect the process when the WebSocket connection closes (and vice-versa).
14+
15+
Any message sent from the WebSocket client will be piped to the process's `STDIN` stream, followed by a `\n` newline.
16+
17+
Any text printed by the process to `STDOUT` shall be sent as a WebSocket message whenever a `\n` newline is encountered.
18+
19+
## Download
20+
21+
If you're on a Mac, you can install `websocketd` using [Homebrew](http://brew.sh/). Just run `brew install websocketd`. For other operating systems, or if you don't want to use Homebrew, check out the link below.
22+
23+
**[Download for Linux, OS X and Windows](https://github.com/app-fast/websocketd/wiki/Download-and-install)**
24+
25+
## Quickstart
26+
27+
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.
28+
29+
To show how simple it is, let's do it in Bash!
30+
31+
**count.sh**:
32+
33+
```sh
34+
#!/bin/bash
35+
for ((COUNT = 1; COUNT <= 10; COUNT++)); do
36+
echo $COUNT
37+
sleep 1
38+
done
39+
```
40+
41+
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.
42+
43+
```sh
44+
$ chmod +x count.sh
45+
$ ./count.sh
46+
1
47+
2
48+
3
49+
4
50+
5
51+
6
52+
7
53+
8
54+
9
55+
10
56+
```
57+
58+
Now let's turn it into a WebSocket server:
59+
60+
```sh
61+
$ websocketd --port=8080 ./count.sh
62+
```
63+
64+
Finally, let's create a web-page to test it.
65+
66+
**count.html**:
67+
68+
```html
69+
<!DOCTYPE html>
70+
<pre id="log"></pre>
71+
<script>
72+
// helper function: log message to screen
73+
function log(msg) {
74+
document.getElementById("log").textContent += msg + "\n";
75+
}
76+
77+
// setup websocket with callbacks
78+
var ws = new WebSocket("ws://localhost:8080/");
79+
ws.onopen = function () {
80+
log("CONNECT");
81+
};
82+
ws.onclose = function () {
83+
log("DISCONNECT");
84+
};
85+
ws.onmessage = function (event) {
86+
log("MESSAGE: " + event.data);
87+
};
88+
</script>
89+
```
90+
91+
Open this page in your web-browser. It will even work if you open it directly
92+
from disk using a `file://` URL.
93+
94+
## More Features
95+
96+
- Very simple install. Just [download](https://github.com/app-fast/websocketd/wiki/Download-and-install) 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.
97+
- 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](https://github.com/app-fast/websocketd/wiki/Environment-variables).
98+
- As well as serving websocket daemons it also includes a static file server and classic CGI server for convenience.
99+
- Command line help available via `websocketd --help`.
100+
- Includes [WebSocket developer console](https://github.com/app-fast/websocketd/wiki/Developer-console) to make it easy to test your scripts before you've built a JavaScript frontend.
101+
- [Examples in many programming languages](https://github.com/app-fast/websocketd/tree/master/examples) are available to help you getting started.
102+
103+
## User Manual
104+
105+
**[More documentation in the user manual](https://github.com/app-fast/websocketd/wiki)**
106+
107+
## Example Projects
108+
109+
- [Plot real time Linux CPU/IO/Mem stats to a HTML5 dashboard using websocketd and vmstat](https://github.com/app-fast/web-vmstats) _(for Linux)_
110+
- [Arbitrary REPL in the browser using websocketd](https://github.com/rowanthorpe/ws-repl)
111+
- [Retrieve SQL data from server with LiveCode and webSocketd](https://github.com/samansjukur/wslc)
112+
- [List files from a configured folder](https://github.com/dbalakirev/directator) _(for Linux)_
113+
- [Listen for gamepad events and report them to the system](https://github.com/experiment322/controlloid-server) _(this + android = gamepad emulator)_
114+
115+
Got more examples? Open a pull request.
116+
117+
## My Other Projects
118+
119+
- [ReconnectingWebSocket](https://github.com/app-fast/reconnecting-websocket) - Simplest way to add some robustness to your WebSocket connections.
120+
- [Smoothie Charts](http://smoothiecharts.org/) - JavaScript charts for streaming data.
121+
- Visit [The Igloo Lab](http://theigloolab.com/) to see and subscribe to other thingies I make.
122+
123+
And [follow @joewalnes](https://twitter.com/joewalnes)!

0 commit comments

Comments
 (0)