Skip to content

Commit

Permalink
jcall: Add websocket support.
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Oct 7, 2021
1 parent c807007 commit 7bcb6aa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
10 changes: 8 additions & 2 deletions tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ module github.com/creachadair/jrpc2/tools

go 1.17

require github.com/creachadair/jrpc2 v0.26.1
require (
github.com/creachadair/jrpc2 v0.26.1
github.com/creachadair/wschannel v0.0.0-20210930050814-ee1a57283ef3
)

require golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
require (
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
)
5 changes: 5 additions & 0 deletions tools/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
github.com/creachadair/jrpc2 v0.26.0/go.mod h1:w+GXZGc+NwsH0xsUOgeLBIIRM0jBOSTXhv28KaWGRZU=
github.com/creachadair/jrpc2 v0.26.1 h1:5i6kkw2hmeocwwFHjNkiQ6jePMufdhcfWOlV6ACU5y8=
github.com/creachadair/jrpc2 v0.26.1/go.mod h1:w+GXZGc+NwsH0xsUOgeLBIIRM0jBOSTXhv28KaWGRZU=
github.com/creachadair/wschannel v0.0.0-20210930050814-ee1a57283ef3 h1:b0LJF4h+tv81wui0UKrszHi+yny5B/UJA10bHimYy0Y=
github.com/creachadair/wschannel v0.0.0-20210930050814-ee1a57283ef3/go.mod h1:ZW4LjPekGnPGBDEgssmCLAOIObRDGv2SQay/pT+5ZwM=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
18 changes: 18 additions & 0 deletions tools/jcall/jcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/creachadair/jrpc2/channel"
"github.com/creachadair/jrpc2/jctx"
"github.com/creachadair/jrpc2/jhttp"
"github.com/creachadair/wschannel"
)

var (
Expand All @@ -49,6 +50,13 @@ Connect to the specified address and transmit the specified JSON-RPC method
calls in sequence (or as a batch, if -batch is set). The resulting response
values are printed to stdout.
Supported address formats include:
host:port -- TCP connection to the given host and port
some/path -- Unix-domain socket at the given path
http://host:port/path -- HTTP connection to the given URL
ws://host:port/path -- Websocket connection to the given URL
Without -m, each pair of arguments names a method and its parameters to call.
With -m, the first argument names a method to be repeatedly called with each of
the remaining arguments as its parameter.
Expand Down Expand Up @@ -112,6 +120,12 @@ func main() {
var cc channel.Channel
if isHTTP(flag.Arg(0)) {
cc = jhttp.NewChannel(flag.Arg(0), nil)
} else if isWebsocket(flag.Arg(0)) {
ch, err := wschannel.Dial(flag.Arg(0), nil)
if err != nil {
log.Fatalf("Dial %q: %v", flag.Arg(0), err)
}
cc = ch
} else if nc := newFraming(*chanFraming); nc == nil {
log.Fatalf("Unknown channel framing %q", *chanFraming)
} else {
Expand Down Expand Up @@ -288,6 +302,10 @@ func isHTTP(addr string) bool {
return strings.HasPrefix(addr, "http:") || strings.HasPrefix(addr, "https:")
}

func isWebsocket(addr string) bool {
return strings.HasPrefix(addr, "ws:") || strings.HasPrefix(addr, "wss:")
}

func callStatus(err error) string {
switch err.(type) {
case nil:
Expand Down

0 comments on commit 7bcb6aa

Please sign in to comment.