2 // Copyright © 2011-2013 Guy M. Allard
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 Receive messages from a STOMP 1.2 broker.
24 "github.com/gmallard/stompngo"
25 "github.com/gmallard/stompngo_examples/sngecomm"
30 var exampid
= "receive_12: "
32 // Connect to a STOMP 1.2 broker, receive some messages and disconnect.
34 fmt
.Println(exampid
+ "starts ...")
36 // Set up the connection.
37 h
, p
:= sngecomm
.HostAndPort12() // A 1.2 connection
38 n
, e
:= net
.Dial("tcp", net
.JoinHostPort(h
, p
))
40 log
.Fatalln(e
) // Handle this ......
42 fmt
.Println(exampid
+ "dial complete ...")
43 ch
:= stompngo
.Headers
{"accept-version", "1.2",
44 "host", sngecomm
.Vhost()}
45 conn
, e
:= stompngo
.Connect(n
, ch
)
47 log
.Fatalln(e
) // Handle this ......
49 fmt
.Println(exampid
+"stomp connect complete ...", conn
.Protocol())
52 u
:= stompngo
.Uuid() // Use package convenience function for unique ID
53 s
:= stompngo
.Headers
{"destination", sngecomm
.Dest(),
54 "id", u
} // subscribe/unsubscribe headers
56 // *NOTE* your application functionaltiy goes here!
57 // With Stomp, you must SUBSCRIBE to a destination in order to receive.
58 // Stomp 1.2 demands subscribing with a unique subscription id, and we
61 // a) A channel of MessageData struct. Note that with this package, the
62 // channel is unique (based on the unique subscription id).
63 // b) A possible error. Always check for errors. They can be logical
64 // errors detected by the stompngo package, or even hard network errors, for
65 // example the broker just crashed.
66 r
, e
:= conn
.Subscribe(s
)
68 log
.Fatalln(e
) // Handle this ...
70 fmt
.Println(exampid
+ "stomp subscribe complete ...")
71 // Read data from the returned channel
72 for i
:= 1; i
<= sngecomm
.Nmsgs(); i
++ {
74 fmt
.Println(exampid
+ "channel read complete ...")
75 // MessageData has two components:
76 // a) a Message struct
77 // b) an Error value. Check the error value as usual
79 log
.Fatalln(m
.Error
) // Handle this
82 fmt
.Printf("Frame Type: %s\n", m
.Message
.Command
) // Will be MESSAGE or ERROR!
83 if m
.Message
.Command
!= stompngo
.MESSAGE
{
84 log
.Fatalln(m
) // Handle this ...
86 h
:= m
.Message
.Headers
87 for j
:= 0; j
< len(h
)-1; j
+= 2 {
88 fmt
.Printf("Header: %s:%s\n", h
[j
], h
[j
+1])
90 fmt
.Printf("Payload: %s\n", string(m
.Message
.Body
)) // Data payload
91 // One item to note: Stomp 1.2 servers _must_ return a 'subscription'
92 // header in each message, where the value of the 'subscription' header
93 // matches the unique subscription id supplied on subscribe. With _some_
94 // stomp client libraries, this allows you to categorize messages by
95 // 'subscription'. That is not required with this package!!! This
96 // because of the unique MessageData channels returned by Subscribe.
97 // But check that this is the case for demonstration purposes.
98 if h
.Value("subscription") != u
{
99 fmt
.Printf("Error condition, expected [%s], received [%s]\n", u
, h
.Value("subscription"))
100 log
.Fatalln("Bad subscription header")
103 // It is polite to unsubscribe, although unnecessary if a disconnect follows.
104 // With Stomp 1.2, the same unique ID is required on UNSUBSCRIBE. Failure
105 // to provide it will result in an error return.
106 e
= conn
.Unsubscribe(s
)
108 log
.Fatalln(e
) // Handle this ...
110 fmt
.Println(exampid
+ "stomp unsubscribe complete ...")
112 // Disconnect from the Stomp server
113 e
= conn
.Disconnect(stompngo
.Headers
{})
115 log
.Fatalln(e
) // Handle this ......
117 fmt
.Println(exampid
+ "stomp disconnect complete ...")
118 // Close the network connection
121 log
.Fatalln(e
) // Handle this ......
123 fmt
.Println(exampid
+ "network close complete ...")
125 fmt
.Println(exampid
+ "ends ...")