Skip to content

Commit

Permalink
Add option for using websocket binary frames instead of text frames
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
isobit committed Nov 9, 2017
1 parent 14b21c4 commit bf07690
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ instead of `STDIN` and `STDOUT`.
## Usage
```
Usage: ws-tcp-relay <tcpTargetAddress>
-b Use binary frames instead of text frames
-binary
Use binary frames instead of text frames
-p uint
The port to listen on (default 4223)
-port uint
Expand All @@ -20,6 +23,10 @@ Usage: ws-tcp-relay <tcpTargetAddress>
TLS key file path
```

### Binary Data
By default, `golang.org/x/net/websocket` uses text frames to deliver payload
data. To use binary frames instead, use either the `b` or `binary` flags.

### WSS Support
To use secure WebSockets simply specify both the `tlscert` and `tlskey` flags.

Expand Down
7 changes: 7 additions & 0 deletions ws-tcp-relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

var tcpAddress string
var binaryMode bool

func copyWorker(dst io.Writer, src io.Reader, doneCh chan<- bool) {
io.Copy(dst, src)
Expand All @@ -26,6 +27,10 @@ func relayHandler(ws *websocket.Conn) {
return
}

if binaryMode {
ws.PayloadType = websocket.BinaryFrame
}

doneCh := make(chan bool)

go copyWorker(conn, ws, doneCh)
Expand All @@ -51,6 +56,8 @@ func main() {
flag.UintVar(&port, "port", 4223, "The port to listen on")
flag.StringVar(&certFile, "tlscert", "", "TLS cert file path")
flag.StringVar(&keyFile, "tlskey", "", "TLS key file path")
flag.BoolVar(&binaryMode, "b", false, "Use binary frames instead of text frames")
flag.BoolVar(&binaryMode, "binary", false, "Use binary frames instead of text frames")
flag.Usage = usage
flag.Parse()

Expand Down

0 comments on commit bf07690

Please sign in to comment.