2 // Copyright © 2017-2018 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.
25 sng
"github.com/gmallard/stompngo"
26 "github.com/gmallard/stompngo/senv"
31 //=========================================================================
32 // Use something like this a boilerplate for connect (Yes, a lot of work,
33 // network connects usually are.)
34 host
, port
:= senv
.HostAndPort()
35 hap
:= net
.JoinHostPort(host
, port
)
36 n
, err
:= net
.Dial(sng
.NetProtoTCP
, hap
)
37 log
.Printf("Connect Host and Port: %s\n", hap
)
38 log
.Printf("Connect Login: %s\n", senv
.Login())
39 log
.Printf("Connect Passcode: %s\n", senv
.Passcode())
41 log
.Fatalln("Net Connect error for:", hap
, "error:", err
)
44 connect_headers
:= sng
.Headers
{sng
.HK_LOGIN
, senv
.Login(),
45 sng
.HK_PASSCODE
, senv
.Passcode(),
46 sng
.HK_VHOST
, senv
.Vhost(),
47 sng
.HK_HEART_BEAT
, senv
.Heartbeats(),
48 sng
.HK_ACCEPT_VERSION
, senv
.Protocol(),
51 stomp_conn
, err
:= sng
.Connect(n
, connect_headers
)
53 log
.Printf("STOMP Connect failed, error:%v\n", err
)
54 if stomp_conn
!= nil {
55 log
.Printf("Connect Response: %v\n", stomp_conn
.ConnectResponse
)
60 //=========================================================================
61 // Use something like this as real application logic
62 fmt
.Printf("Client CONNECT Headers:\n%v\n", connect_headers
)
63 fmt
.Printf("Broker CONNECTED Data:\n")
64 fmt
.Printf("Server: %s\n",
65 stomp_conn
.ConnectResponse
.Headers
.Value(sng
.HK_SERVER
))
66 fmt
.Printf("Protocol: %s\n",
67 stomp_conn
.ConnectResponse
.Headers
.Value(sng
.HK_VERSION
))
68 fmt
.Printf("Heartbeats: %s\n",
69 stomp_conn
.ConnectResponse
.Headers
.Value(sng
.HK_HEART_BEAT
))
70 fmt
.Printf("Session: %s\n",
71 stomp_conn
.ConnectResponse
.Headers
.Value(sng
.HK_SESSION
))
74 //=========================================================================
75 // Use something like this as boilerplate for disconnect (Clean disconnects
76 // are also a lot of work.)
77 err
= stomp_conn
.Disconnect(sng
.Headers
{})
79 log
.Fatalf("DISCONNECT Failed, error:%v\n", err
)
83 log
.Fatalf("Net Close Error:%v\n", err
)