Skip to content

Commit

Permalink
Merge pull request #797 from strukturag/dependabot/go_modules/github.…
Browse files Browse the repository at this point in the history
…com/nats-io/nats.go-1.37.0

Bump github.com/nats-io/nats.go from 1.36.0 to 1.37.0
  • Loading branch information
fancycode authored Aug 29, 2024
2 parents 8b05abd + b06f5a8 commit f71a2bb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/mailru/easyjson v0.7.7
github.com/marcw/cachecontrol v0.0.0-20140722115028-30341fe9a7d5
github.com/nats-io/nats-server/v2 v2.10.19
github.com/nats-io/nats.go v1.36.0
github.com/nats-io/nats.go v1.37.0
github.com/notedit/janus-go v0.0.0-20200517101215-10eb8b95d1a0
github.com/oschwald/maxminddb-golang v1.13.1
github.com/pion/sdp/v3 v3.0.9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ github.com/nats-io/jwt/v2 v2.5.8 h1:uvdSzwWiEGWGXf+0Q+70qv6AQdvcvxrv9hPM0RiPamE=
github.com/nats-io/jwt/v2 v2.5.8/go.mod h1:ZdWS1nZa6WMZfFwwgpEaqBV8EPGVgOTDHN/wTbz0Y5A=
github.com/nats-io/nats-server/v2 v2.10.19 h1:zf+B16ZbzzGGogvDwCOjSCR5776kix/JgF84TiWNSQk=
github.com/nats-io/nats-server/v2 v2.10.19/go.mod h1:hgcPnoUtMfxz1qVOvLZGurVypQ+Cg6GXVXjG53iHk+M=
github.com/nats-io/nats.go v1.36.0 h1:suEUPuWzTSse/XhESwqLxXGuj8vGRuPRoG7MoRN/qyU=
github.com/nats-io/nats.go v1.36.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8=
github.com/nats-io/nats.go v1.37.0 h1:07rauXbVnnJvv1gfIyghFEo6lUcYRY0WXc3x7x0vUxE=
github.com/nats-io/nats.go v1.37.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8=
github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI=
github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
Expand Down
43 changes: 31 additions & 12 deletions natsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ package signaling
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"strings"
"time"

"github.com/nats-io/nats.go"
Expand Down Expand Up @@ -61,8 +63,7 @@ func GetEncodedSubject(prefix string, suffix string) string {
}

type natsClient struct {
nc *nats.Conn
conn *nats.EncodedConn
conn *nats.Conn
}

func NewNatsClient(url string) (NatsClient, error) {
Expand All @@ -82,7 +83,7 @@ func NewNatsClient(url string) (NatsClient, error) {

client := &natsClient{}

client.nc, err = nats.Connect(url,
client.conn, err = nats.Connect(url,
nats.ClosedHandler(client.onClosed),
nats.DisconnectHandler(client.onDisconnected),
nats.ReconnectHandler(client.onReconnected))
Expand All @@ -98,12 +99,9 @@ func NewNatsClient(url string) (NatsClient, error) {
return nil, fmt.Errorf("interrupted")
}

client.nc, err = nats.Connect(url)
client.conn, err = nats.Connect(url)
}
log.Printf("Connection established to %s (%s)", client.nc.ConnectedUrl(), client.nc.ConnectedServerId())

// All communication will be JSON based.
client.conn, _ = nats.NewEncodedConn(client.nc, nats.JSON_ENCODER)
log.Printf("Connection established to %s (%s)", client.conn.ConnectedUrl(), client.conn.ConnectedServerId())
return client, nil
}

Expand All @@ -124,13 +122,34 @@ func (c *natsClient) onReconnected(conn *nats.Conn) {
}

func (c *natsClient) Subscribe(subject string, ch chan *nats.Msg) (NatsSubscription, error) {
return c.nc.ChanSubscribe(subject, ch)
return c.conn.ChanSubscribe(subject, ch)
}

func (c *natsClient) Publish(subject string, message interface{}) error {
return c.conn.Publish(subject, message)
data, err := json.Marshal(message)
if err != nil {
return err
}

return c.conn.Publish(subject, data)
}

func (c *natsClient) Decode(msg *nats.Msg, v interface{}) error {
return c.conn.Enc.Decode(msg.Subject, msg.Data, v)
func (c *natsClient) Decode(msg *nats.Msg, vPtr interface{}) (err error) {
switch arg := vPtr.(type) {
case *string:
// If they want a string and it is a JSON string, strip quotes
// This allows someone to send a struct but receive as a plain string
// This cast should be efficient for Go 1.3 and beyond.
str := string(msg.Data)
if strings.HasPrefix(str, `"`) && strings.HasSuffix(str, `"`) {
*arg = str[1 : len(str)-1]
} else {
*arg = str
}
case *[]byte:
*arg = msg.Data
default:
err = json.Unmarshal(msg.Data, arg)
}
return
}

0 comments on commit f71a2bb

Please sign in to comment.