本文整理匯總了Golang中github.com/cloudfoundry/yagnats.NATSConn.Publish方法的典型用法代碼示例。如果您正苦於以下問題:Golang NATSConn.Publish方法的具體用法?Golang NATSConn.Publish怎麽用?Golang NATSConn.Publish使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/cloudfoundry/yagnats.NATSConn
的用法示例。
在下文中一共展示了NATSConn.Publish方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Register
func (c *VcapComponent) Register(mbusClient yagnats.NATSConn) error {
mbusClient.Subscribe("vcap.component.discover", func(msg *nats.Msg) {
if msg.Reply == "" {
log.Warnf("Received message with empty reply on subject %s", msg.Subject)
return
}
c.Uptime = c.StartTime.Elapsed()
b, e := json.Marshal(c)
if e != nil {
log.Warnf(e.Error())
return
}
mbusClient.Publish(msg.Reply, b)
})
b, e := json.Marshal(c)
if e != nil {
log.Error(e.Error())
return e
}
mbusClient.Publish("vcap.component.announce", b)
log.Infof("Component %s registered successfully", c.Type)
return nil
}
示例2: StartFakeRouter
func StartFakeRouter() {
var startMessage = func() []byte {
d := RouterStart{
MinimumRegisterIntervalInSeconds: 20,
}
value, _ := json.Marshal(d)
return value
}
natsMembers := make([]string, 1)
uri := url.URL{
Scheme: "nats",
User: url.UserPassword("", ""),
Host: "127.0.0.1:4222",
}
natsMembers = append(natsMembers, uri.String())
var natsClient yagnats.NATSConn
var err error
for i := 0; i < 10; i++ {
natsClient, err = yagnats.Connect(natsMembers)
if err == nil {
break
}
time.Sleep(time.Second)
}
Expect(err).ToNot(HaveOccurred())
natsClient.Subscribe("router.register", func(msg *nats.Msg) {
})
natsClient.Subscribe("router.greet", func(msg *nats.Msg) {
natsClient.Publish(msg.Reply, startMessage())
})
natsClient.Publish("router.start", startMessage())
}
示例3:
Expect(err).ToNot(HaveOccurred())
Expect(strings.Contains(line, "100 Continue")).To(BeTrue())
var rr *http.Request
Eventually(rCh).Should(Receive(&rr))
Expect(rr).ToNot(BeNil())
Expect(rr.Header.Get("Expect")).To(Equal(""))
})
It("handles a /routes request", func() {
var client http.Client
var req *http.Request
var resp *http.Response
var err error
mbusClient.Publish("router.register", []byte(`{"dea":"dea1","app":"app1","uris":["test.com"],"host":"1.2.3.4","port":1234,"tags":{},"private_instance_id":"private_instance_id"}`))
time.Sleep(250 * time.Millisecond)
host := fmt.Sprintf("http://%s:%d/routes", config.Ip, config.Status.Port)
req, err = http.NewRequest("GET", host, nil)
req.SetBasicAuth("user", "pass")
resp, err = client.Do(req)
Expect(err).ToNot(HaveOccurred())
Expect(resp).ToNot(BeNil())
Expect(resp.StatusCode).To(Equal(200))
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
Expect(err).ToNot(HaveOccurred())