update json encoder
This commit is contained in:
		
							parent
							
								
									b82c017596
								
							
						
					
					
						commit
						0eb670a0c5
					
				| 
						 | 
					@ -0,0 +1,26 @@
 | 
				
			||||||
 | 
					package natsio
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/nats-io/nats.go"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// JSONEncoder ...
 | 
				
			||||||
 | 
					type JSONEncoder struct {
 | 
				
			||||||
 | 
						encConn *nats.EncodedConn
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Subscribe ...
 | 
				
			||||||
 | 
					func (e JSONEncoder) Subscribe(subject string, cb nats.Handler) (*nats.Subscription, error) {
 | 
				
			||||||
 | 
						sub, err := e.encConn.Subscribe(subject, cb)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Printf("natsio.JSONEncoder.Subscribe err: %v\n", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return sub, err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Publish ...
 | 
				
			||||||
 | 
					func (e JSONEncoder) Publish(reply string, data interface{}) error {
 | 
				
			||||||
 | 
						return e.encConn.Publish(reply, data)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -3,6 +3,7 @@ package natsio
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/nats-io/nats.go"
 | 
						"github.com/nats-io/nats.go"
 | 
				
			||||||
| 
						 | 
					@ -36,6 +37,11 @@ func (sv Server) Subscribe(subject string, cb nats.MsgHandler) (*nats.Subscripti
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewJSONEncodedConn ...
 | 
					// NewJSONEncodedConn ...
 | 
				
			||||||
func (sv Server) NewJSONEncodedConn() (*nats.EncodedConn, error) {
 | 
					func (sv Server) NewJSONEncodedConn() (*JSONEncoder, error) {
 | 
				
			||||||
	return nats.NewEncodedConn(sv.instance, nats.JSON_ENCODER)
 | 
						enc, err := nats.NewEncodedConn(sv.instance, nats.JSON_ENCODER)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Printf("natsio.NewJSONEncodedConn: err %v\n", err)
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return &JSONEncoder{encConn: enc}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue