Version bump.
[stompngo_examples.git] / conndisc_tls / conndisc_tls.go
blob9e3423add8dea38ed4c3210a353e975df5e0154d
1 //
2 // Copyright © 2013-2018 Guy M. Allard
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
23 Example:
25 # Connect to a broker using TLS. Note: this is a local TLS port.
26 # The broker does not require authentication.
27 STOMP_PORT=61611 go run conndisc_tls.go
30 package main
32 import (
33 "crypto/tls"
34 "log"
35 "os"
36 "time"
37 // sngecomm methods are used specifically for these example clients.
38 "github.com/gmallard/stompngo_examples/sngecomm"
41 var (
42 exampid = "conndisc_tls: "
43 tc *tls.Config
44 ll = log.New(os.Stdout, "TLCD ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
45 tag = "tcdmain"
48 // Connect to a STOMP broker using TLS and disconnect.
49 func main() {
51 st := time.Now()
53 ll.Printf("%stag:%s connsess:%s starts\n",
54 exampid, tag, sngecomm.Lcs)
56 // TLS Configuration. This configuration assumes that:
57 // a) The server used does *not* require client certificates
58 // b) This client has no need to authenticate the server
59 // Note that the tls.Config structure can be modified to support any
60 // authentication scheme, including two-way/mutual authentication. Examples
61 // are provided elsewhere in this project.
62 tc = new(tls.Config)
63 tc.InsecureSkipVerify = true // Do *not* check the server's certificate
65 // Standard example TLS connect sequence
66 n, conn, e := sngecomm.CommonTLSConnect(exampid, tag, ll, tc)
67 if e != nil {
68 ll.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
69 exampid, tag, sngecomm.Lcs,
70 e.Error()) // Handle this ......
73 nc := n.(*tls.Conn)
74 sngecomm.DumpTLSConfig(exampid, tc, nc)
76 // *NOTE* application specific functionaltiy starts here!
77 // For you to add.
78 // *NOTE* application specific functionaltiy ends here!
80 // Standard example disconnect sequence
81 e = sngecomm.CommonDisconnect(n, conn, exampid, tag, ll)
82 if e != nil {
83 ll.Fatalf("%s %s\n", exampid, e.Error()) // Handle this ......
86 ll.Printf("%stag:%s connsess:%s main_elapsed:%v\n",
87 exampid, tag, conn.Session(),
88 time.Now().Sub(st))