本文整理汇总了Golang中github.com/apcera/nats.Connect函数的典型用法代码示例。如果您正苦于以下问题:Golang Connect函数的具体用法?Golang Connect怎么用?Golang Connect使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Connect函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ExampleConnect
// Shows different ways to create a Conn
func ExampleConnect() {
nats.Connect(nats.DefaultURL)
nats.Connect("nats://derek:[email protected]:421")
opts := nats.Options{
AllowReconnect: true,
MaxReconnect: 10,
ReconnectWait: 5 * time.Second,
Timeout: 1 * time.Second,
}
nc, _ := opts.Connect()
nc.Close()
}
示例2: NewGnatsd
func NewGnatsd(numberOfMessages int, testLatency bool) *Gnatsd {
conn, _ := nats.Connect(nats.DefaultURL)
// We want to be alerted if we get disconnected, this will
// be due to Slow Consumer.
conn.Opts.AllowReconnect = false
// Report async errors.
conn.Opts.AsyncErrorCB = func(nc *nats.Conn, sub *nats.Subscription, err error) {
panic(fmt.Sprintf("NATS: Received an async error! %v\n", err))
}
// Report a disconnect scenario.
conn.Opts.DisconnectedCB = func(nc *nats.Conn) {
fmt.Printf("Getting behind! %d\n", nc.OutMsgs-nc.InMsgs)
panic("NATS: Got disconnected!")
}
var handler benchmark.MessageHandler
if testLatency {
handler = &benchmark.LatencyMessageHandler{
NumberOfMessages: numberOfMessages,
Latencies: []float32{},
}
} else {
handler = &benchmark.ThroughputMessageHandler{NumberOfMessages: numberOfMessages}
}
return &Gnatsd{
handler: handler,
subject: "test",
conn: conn,
testLatency: testLatency,
}
}
示例3: ExampleConn_PublishMsg
func ExampleConn_PublishMsg() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
msg := &nats.Msg{Subject: "foo", Reply: "bar", Data: []byte("Hello World!")}
nc.PublishMsg(msg)
}
示例4: Dial
func (n *ntport) Dial(addr string, opts ...transport.DialOption) (transport.Client, error) {
cAddr := nats.DefaultURL
if len(n.addrs) > 0 && strings.HasPrefix(n.addrs[0], "nats://") {
cAddr = n.addrs[0]
}
c, err := nats.Connect(cAddr)
if err != nil {
return nil, err
}
id := nats.NewInbox()
sub, err := c.SubscribeSync(id)
if err != nil {
return nil, err
}
return &ntportClient{
conn: c,
addr: addr,
id: id,
sub: sub,
}, nil
}
示例5: ExampleConn_Subscribe
// This Example shows an asynchronous subscriber.
func ExampleConn_Subscribe() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
nc.Subscribe("foo", func(m *nats.Msg) {
fmt.Printf("Received a message: %s\n", string(m.Data))
})
}
示例6: ExampleSubscription_Unsubscribe
func ExampleSubscription_Unsubscribe() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
sub, _ := nc.SubscribeSync("foo")
// ...
sub.Unsubscribe()
}
示例7: ExampleConn_Request
func ExampleConn_Request() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
nc.Subscribe("foo", func(m *nats.Msg) {
nc.Publish(m.Reply, []byte("I will help you"))
})
nc.Request("foo", []byte("help"), 50*time.Millisecond)
}
示例8: main
func main() {
nc, _ := nats.Connect("nats://yourhost:4222")
defer nc.Close()
nc.QueueSubscribe(TOPIC, QUEUE, func(m *nats.Msg) {
fmt.Println(string(m.Data))
open.Start(string(m.Data))
})
select {}
}
示例9: Open
func (b Bopen) Open(url string) {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
if b.Topic != "" {
nc.Publish(b.Topic, []byte(url))
} else {
nc.Publish("url", []byte(url))
}
}
示例10: ExampleConn_QueueSubscribe
func ExampleConn_QueueSubscribe() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
received := 0
nc.QueueSubscribe("foo", "worker_group", func(_ *nats.Msg) {
received += 1
})
}
示例11: Init
func (nc *NatsClient) Init(server, service string) {
var err error
nc.natsConn, err = nats.Connect(server)
if err != nil {
panic(err)
}
nc.natsEnConn, err = nats.NewEncodedConn(nc.natsConn, nats.JSON_ENCODER)
if err != nil {
panic(err)
}
//defer ec.Close()
}
示例12: ExampleSubscription_NextMsg
func ExampleSubscription_NextMsg() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
sub, _ := nc.SubscribeSync("foo")
m, err := sub.NextMsg(1 * time.Second)
if err == nil {
fmt.Printf("Received a message: %s\n", string(m.Data))
} else {
fmt.Println("NextMsg timed out.")
}
}
示例13: ExampleConn_Flush
func ExampleConn_Flush() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
msg := &nats.Msg{Subject: "foo", Reply: "bar", Data: []byte("Hello World!")}
for i := 0; i < 1000; i++ {
nc.PublishMsg(msg)
}
err := nc.Flush()
if err == nil {
// Everything has been processed by the server for nc *Conn.
}
}
示例14: ExampleEncodedConn_Publish
// EncodedConn can publish virtually anything just
// by passing it in. The encoder will be used to properly
// encode the raw Go type
func ExampleEncodedConn_Publish() {
nc, _ := nats.Connect(nats.DefaultURL)
c, _ := nats.NewEncodedConn(nc, "json")
defer c.Close()
type person struct {
Name string
Address string
Age int
}
me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
c.Publish("hello", me)
}
示例15: ExampleEncodedConn_BindSendChan
// BindSendChan() allows binding of a Go channel to a nats
// subject for publish operations. The Encoder attached to the
// EncodedConn will be used for marshalling.
func ExampleEncodedConn_BindSendChan() {
nc, _ := nats.Connect(nats.DefaultURL)
c, _ := nats.NewEncodedConn(nc, "json")
defer c.Close()
type person struct {
Name string
Address string
Age int
}
ch := make(chan *person)
c.BindSendChan("hello", ch)
me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
ch <- me
}