當前位置: 首頁>>代碼示例>>Golang>>正文


Golang nats.Connect函數代碼示例

本文整理匯總了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()
}
開發者ID:MarkyV,項目名稱:nats,代碼行數:16,代碼來源:example_test.go

示例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,
	}
}
開發者ID:hitomi333,項目名稱:mq-benchmarking,代碼行數:35,代碼來源:gnatsd.go

示例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)
}
開發者ID:MarkyV,項目名稱:nats,代碼行數:7,代碼來源:example_test.go

示例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
}
開發者ID:kgrvamsi,項目名稱:go-micro,代碼行數:25,代碼來源:nats.go

示例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))
	})
}
開發者ID:MarkyV,項目名稱:nats,代碼行數:9,代碼來源:example_test.go

示例6: ExampleSubscription_Unsubscribe

func ExampleSubscription_Unsubscribe() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	sub, _ := nc.SubscribeSync("foo")
	// ...
	sub.Unsubscribe()
}
開發者ID:MarkyV,項目名稱:nats,代碼行數:8,代碼來源:example_test.go

示例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)
}
開發者ID:MarkyV,項目名稱:nats,代碼行數:9,代碼來源:example_test.go

示例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 {}
}
開發者ID:anykao,項目名稱:p,代碼行數:9,代碼來源:bopen.go

示例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))
	}
}
開發者ID:anykao,項目名稱:p,代碼行數:9,代碼來源:bopen_linux.go

示例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
	})
}
開發者ID:MarkyV,項目名稱:nats,代碼行數:10,代碼來源:example_test.go

示例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()
}
開發者ID:sbinq,項目名稱:goatt,代碼行數:12,代碼來源:client_nats.go

示例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.")
	}
}
開發者ID:MarkyV,項目名稱:nats,代碼行數:12,代碼來源:example_test.go

示例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.
	}
}
開發者ID:MarkyV,項目名稱:nats,代碼行數:13,代碼來源:example_test.go

示例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)
}
開發者ID:MarkyV,項目名稱:nats,代碼行數:17,代碼來源:example_test.go

示例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
}
開發者ID:hsinhoyeh,項目名稱:nats,代碼行數:20,代碼來源:example_test.go


注:本文中的github.com/apcera/nats.Connect函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。