Copyright date updates.
[stompngo_examples.git] / conndisc / conndisc.go
blob41666934969b99e29f5744d7a44395c91e3afe02
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 TCP.
20 Examples:
22 # Connect to a broker with all defaults:
23 # Host is "localhost"
24 # Port is 61613
25 # Login is "guest"
26 # Passcode is "guest
27 # Virtual Host is "localhost"
28 # Protocol is 1.1
29 go run conndisc.go
31 # Connect to a broker using STOMP protocol level 1.0:
32 STOMP_PROTOCOL=1.0 go run conndisc.go
34 # Connect to a broker using a custom host and port:
35 STOMP_HOST=tjjackson STOMP_PORT=62613 go run conndisc.go
37 # Connect to a broker using a custom port and virtual host:
38 STOMP_PORT=41613 STOMP_VHOST="/" go run conndisc.go
40 # Connect to a broker using a custom login and passcode:
41 STOMP_LOGIN="userid" STOMP_PASSCODE="t0ps3cr3t" go run conndisc.go
44 package main
46 import (
47 "log"
48 "os"
49 "time"
50 // sngecomm methods are used specifically for these example clients.
51 "github.com/gmallard/stompngo_examples/sngecomm"
54 var (
55 exampid = "conndisc: "
56 ll = log.New(os.Stdout, "ECNDS ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
57 tag = "condiscmain"
60 // Connect to a STOMP broker and disconnect.
61 func main() {
63 st := time.Now()
65 // Standard example connect sequence
66 n, conn, e := sngecomm.CommonConnect(exampid, tag, ll)
67 if e != nil {
68 if conn != nil {
69 ll.Printf("%stag%s connsess:%s main_on_connect error Connect Response headers:%v body%s\n",
70 exampid, tag, conn.Session(), conn.ConnectResponse.Headers,
71 string(conn.ConnectResponse.Body))
73 ll.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
74 exampid, tag, sngecomm.Lcs,
75 e.Error()) // Handle this ......
78 ll.Printf("%stag%s connsess:%s Connect Response headers:%v body%s\n",
79 exampid, tag, conn.Session(), conn.ConnectResponse.Headers,
80 string(conn.ConnectResponse.Body))
82 // *NOTE* application specific functionaltiy starts here!
83 // For you to add.
84 // *NOTE* application specific functionaltiy ends here!
86 // Standard example disconnect sequence
87 e = sngecomm.CommonDisconnect(n, conn, exampid, tag, ll)
88 if e != nil {
89 ll.Fatalf("%stag:%s connsess:%s main_on_disconnect error:%v",
90 exampid, tag, conn.Session(),
91 e.Error()) // Handle this ......
94 ll.Printf("%stag:%s connsess:%s main_elapsed:%v\n",
95 exampid, tag, conn.Session(),
96 time.Now().Sub(st))