MCPcopy
hub / github.com/nats-io/nats.go / main

Function main

examples/nats-req/main.go:38–96  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

36}
37
38func main() {
39 var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
40 var userCreds = flag.String("creds", "", "User Credentials File")
41 var nkeyFile = flag.String("nkey", "", "NKey Seed File")
42 var showHelp = flag.Bool("h", false, "Show help message")
43
44 log.SetFlags(0)
45 flag.Usage = usage
46 flag.Parse()
47
48 if *showHelp {
49 showUsageAndExit(0)
50 }
51
52 args := flag.Args()
53 if len(args) < 2 {
54 showUsageAndExit(1)
55 }
56
57 // Connect Options.
58 opts := []nats.Option{nats.Name("NATS Sample Requestor")}
59
60 if *userCreds != "" && *nkeyFile != "" {
61 log.Fatal("specify -seed or -creds")
62 }
63
64 // Use UserCredentials
65 if *userCreds != "" {
66 opts = append(opts, nats.UserCredentials(*userCreds))
67 }
68
69 // Use Nkey authentication.
70 if *nkeyFile != "" {
71 opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
72 if err != nil {
73 log.Fatal(err)
74 }
75 opts = append(opts, opt)
76 }
77
78 // Connect to NATS
79 nc, err := nats.Connect(*urls, opts...)
80 if err != nil {
81 log.Fatal(err)
82 }
83 defer nc.Close()
84 subj, payload := args[0], []byte(args[1])
85
86 msg, err := nc.Request(subj, payload, 2*time.Second)
87 if err != nil {
88 if nc.LastError() != nil {
89 log.Fatalf("%v for request", nc.LastError())
90 }
91 log.Fatalf("%v for request", err)
92 }
93
94 log.Printf("Published [%s] : '%s'", subj, payload)
95 log.Printf("Received [%v] : '%s'", msg.Subject, string(msg.Data))

Callers

nothing calls this directly

Calls 8

ConnectMethod · 0.80
FatalfMethod · 0.80
showUsageAndExitFunction · 0.70
NameMethod · 0.65
StringMethod · 0.45
CloseMethod · 0.45
RequestMethod · 0.45
LastErrorMethod · 0.45

Tested by

no test coverage detected