2 // Copyright © 2013-2016 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 Connect and Disconnect from a STOMP broker using TLS.
20 All examples in the 'conndisc' directory also apply here. This example shows
21 that TLS is requested by using a specific port and tls.Dial.
25 # Connect to a broker using TLS:
26 STOMP_PORT=61612 go run conndisc_tls.go
37 "github.com/gmallard/stompngo"
38 // senv methods could be used in general by stompngo clients.
39 "github.com/gmallard/stompngo/senv"
40 // sngecomm methods are used specifically for these example clients.
41 "github.com/gmallard/stompngo_examples/sngecomm"
45 exampid
= "conndisc_tls: "
47 ll
= log
.New(os
.Stdout
, "TLCD ", log
.Ldate|log
.Lmicroseconds|log
.Lshortfile
)
50 // Connect to a STOMP 1.0 broker using TLS and disconnect.
52 ll
.Println(exampid
+ "starts ...")
54 // TLS Configuration. This configuration assumes that:
55 // a) The server used does *not* require client certificates
56 // b) This client has no need to authenticate the server
57 // Note that the tls.Config structure can be modified to support any
58 // authentication scheme, including two-way/mutual authentication. Examples
59 // are provided elsewhere in this project.
61 tc
.InsecureSkipVerify
= true // Do *not* check the server's certificate
63 // Open a net connection
64 h
, p
:= senv
.HostAndPort()
65 // Use tls.Dial, not net.Dial
66 n
, e
:= tls
.Dial("tcp", net
.JoinHostPort(h
, p
), tc
)
68 ll
.Fatalln(e
) // Handle this ......
70 ll
.Println(exampid
+"dial complete ...", net
.JoinHostPort(h
, p
))
72 // All stomp API methods require 'Headers'. Stomp headers are key/value
73 // pairs. The stompngo package implements them using a string slice.
74 ch
:= sngecomm
.ConnectHeaders()
76 // Get a stomp connection. Parameters are:
77 // a) the opened net connection
79 conn
, e
:= stompngo
.Connect(n
, ch
)
81 ll
.Fatalln(e
) // Handle this ......
83 ll
.Println(exampid
+ "stomp connect complete ...")
85 // *NOTE* your application functionaltiy goes here!
87 // Stomp disconnects are not required, but highly recommended.
88 // We use empty headers here for the DISCONNECT. This will trigger the
89 // disconnect receipt described below.
91 // If you do *not* want a disconnect receipt use:
92 // stompngo.Headers{"noreceipt", "true"}
94 e
= conn
.Disconnect(stompngo
.Headers
{})
96 ll
.Fatalln(exampid
+"sngDisconnectTLS", e
) // Handle this ......
98 ll
.Println(exampid
+ "stomp disconnect complete ...")
100 // After a DISCONNECT there is (by default) a 'Disconnect
101 // Receipt' available. The disconnect receipt is an instance of
102 // stompngo.MessageData.
103 ll
.Println(exampid
+"disconnect receipt", conn
.DisconnectReceipt
)
104 ll
.Println(exampid
+ "stomp disconnect complete ...")
106 // Close the net connection.
109 ll
.Fatalln(e
) // Handle this ......
111 ll
.Println(exampid
+ "network close complete ...")
113 ll
.Println(exampid
+ "ends ...")