Skip to content

Commit

Permalink
Adds single response mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Oct 30, 2023
1 parent 60a56da commit 7eb3b55
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 8 additions & 1 deletion cmd/wsget/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var insecure bool
var request string
var outputFile string
var headers []string
var singleResponseTimeout int

const (
LongDescription = `A command-line tool for interacting with WebSocket servers.
Expand Down Expand Up @@ -52,6 +53,7 @@ func main() {
cmd.Flags().BoolVarP(&insecure, "insecure", "k", false, "Skip SSL certificate verification")
cmd.Flags().StringVarP(&request, "request", "r", "", "WebSocket request that will be sent to the server")
cmd.Flags().StringVarP(&outputFile, "output", "o", "", "Output file for saving all request and responses")
cmd.Flags().IntVarP(&singleResponseTimeout, "single-resp", "S", -1, "Timeout for single response in seconds, 0 means no timeout. If this option is set, the tool will exit after receiving the first response")
cmd.Flags().StringSliceVarP(&headers, "header", "H", []string{}, "HTTP headers to attach to the request")

err := cmd.Execute()
Expand All @@ -68,7 +70,12 @@ func run(cmd *cobra.Command, args []string) {
return
}

wsConn, err := ws.NewWS(wsURL, ws.Options{SkipSSLVerification: insecure, Headers: headers})
if singleResponseTimeout > 0 && request == "" {
color.New(color.FgRed).Println("Single response timeout could be used only with request")
return
}

wsConn, err := ws.NewWS(wsURL, ws.Options{SkipSSLVerification: insecure, Headers: headers, WaitForResp: singleResponseTimeout})
if err != nil {
color.New(color.FgRed).Println("Unable to connect to the server: ", err)
return
Expand Down
17 changes: 16 additions & 1 deletion pkg/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"strings"
"time"

"github.com/fatih/color"
"golang.org/x/net/websocket"
Expand Down Expand Up @@ -47,6 +48,7 @@ type Connection struct {
type Options struct {
Headers []string
SkipSSLVerification bool
WaitForResp int
}

func NewWS(url string, opts Options) (*Connection, error) {
Expand Down Expand Up @@ -82,6 +84,13 @@ func NewWS(url string, opts Options) (*Connection, error) {

ws, err := websocket.DialConfig(cfg)

if opts.WaitForResp > 0 {
go func() {
time.Sleep(time.Duration(opts.WaitForResp) * time.Second)
ws.Close()
}()
}

if err != nil {
return nil, err
}
Expand All @@ -96,7 +105,9 @@ func NewWS(url string, opts Options) (*Connection, error) {

err = websocket.Message.Receive(ws, &msg)
if err != nil {
if err.Error() == "EOF" {
if opts.WaitForResp >= 0 {

Check failure on line 108 in pkg/ws/ws.go

View workflow job for this annotation

GitHub Actions / tests

ifElseChain: rewrite if-else to switch statement (gocritic)
return
} else if err.Error() == "EOF" {
color.New(color.FgRed).Println("Connection closed by the server")
} else {
color.New(color.FgRed).Println("Fail read from connection: ", err)
Expand All @@ -106,6 +117,10 @@ func NewWS(url string, opts Options) (*Connection, error) {
}

messages <- Message{Type: Response, Data: msg}

if opts.WaitForResp >= 0 {
return
}
}
}(messages)

Expand Down

0 comments on commit 7eb3b55

Please sign in to comment.