Skip to content

Commit

Permalink
Add protobuf support
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Sep 15, 2021
1 parent fabf717 commit 1ace579
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,12 @@ You can pass the following options to the `connect` method as the second argumen
handshakeTimeoutS: 60, // Max allowed time to initialize a connection
receiveTimeoutMs: 1000, // Max time to wait for an incoming message
logLevel: "info" // logging level (change to debug to see more information)
codec: "json", // Codec (encoder) to use. Supported values are: json, msgpack.
codec: "json", // Codec (encoder) to use. Supported values are: json, msgpack, protobuf.
}
```
**NOTE:** `msgpack` and `protobuf` codecs are only supported by [AnyCable PRO](https://anycable.io#pro).
More examples could be found in the [examples/](./examples) folder.
## Contributing
Expand Down
208 changes: 208 additions & 0 deletions ac_protos/action_cable.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cable.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func (r *Cable) Connect(ctx context.Context, cableUrl string, opts goja.Value) (
headers.Set("Sec-WebSocket-Protocol", "actioncable-v1-json")
} else if cOpts.codec() == MsgPackCodec {
headers.Set("Sec-WebSocket-Protocol", "actioncable-v1-msgpack")
} else if cOpts.codec() == ProtobufCodec {
headers.Set("Sec-WebSocket-Protocol", "actioncable-v1-protobuf")
}

level, err := logrus.ParseLevel(cOpts.LogLevel)
Expand Down
67 changes: 67 additions & 0 deletions codec.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package cable

import (
"fmt"
"io/ioutil"

"github.com/golang/protobuf/proto"
"github.com/gorilla/websocket"
"github.com/vmihailenco/msgpack/v5"

pb "github.com/anycable/xk6-cable/ac_protos"
)

type Codec struct {
Expand Down Expand Up @@ -44,3 +50,64 @@ var MsgPackCodec = &Codec{
return err2
},
}

var ProtobufCodec = &Codec{
Receive: func(c *websocket.Conn, v interface{}) error {
mtype, r, err := c.NextReader()
if err != nil {
return err
}

if mtype != websocket.BinaryMessage {
return fmt.Errorf("Unexpected message type: %v", mtype)
}

raw, err := ioutil.ReadAll(r)

if err != nil {
return err
}

buf := &pb.Message{}
if err := proto.Unmarshal(raw, buf); err != nil {
return err
}

msg := (v).(*cableMsg)

msg.Type = buf.Type.String()
msg.Identifier = buf.Identifier

if buf.Message != nil {
var message interface{}

err = msgpack.Unmarshal(buf.Message, &message)
msg.Message = message
}

return nil
},
Send: func(c *websocket.Conn, v interface{}) error {
msg := (v).(*cableMsg)

buf := &pb.Message{}

buf.Command = pb.Command(pb.Command_value[msg.Command])
buf.Identifier = msg.Identifier
buf.Data = msg.Data

b, err := proto.Marshal(buf)
if err != nil {
return err
}

w, err := c.NextWriter(websocket.BinaryMessage)
if err != nil {
return err
}

w.Write(b)
err = w.Close()
return err
},
}
2 changes: 2 additions & 0 deletions connect_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func parseOptions(ctx context.Context, inOpts goja.Value) (*connectOptions, erro
func (co *connectOptions) codec() *Codec {
if co.Codec == "msgpack" {
return MsgPackCodec
} else if co.Codec == "protobuf" {
return ProtobufCodec
}

return JSONCodec
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/dop251/goja v0.0.0-20210427212725-462d53687b0d
github.com/golang/protobuf v1.5.2
github.com/gorilla/websocket v1.4.2
github.com/sirupsen/logrus v1.8.1
github.com/vmihailenco/msgpack/v5 v5.3.2
Expand Down
11 changes: 8 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
Expand All @@ -125,8 +127,9 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -510,8 +513,10 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.25.1-0.20200805231151-a709e31e5d12 h1:OwhZOOMuf7leLaSCuxtQ9FW7ui2L2L6UKOtKAUqovUQ=
google.golang.org/protobuf v1.25.1-0.20200805231151-a709e31e5d12/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit 1ace579

Please sign in to comment.