Exemplo n.º 1
0
// Provide connect headers
func ConnectHeaders() stompngo.Headers {
	h := stompngo.Headers{}
	l := senv.Login()
	if l != "" {
		h = h.Add("login", l)
	}
	pc := senv.Passcode()
	if pc != "" {
		h = h.Add("passcode", pc)
	}
	//
	p := senv.Protocol()
	if p != stompngo.SPL_10 { // 1.1 and 1.2
		h = h.Add("accept-version", p).Add("host", senv.Vhost())
		hb := senv.Heartbeats()
		if hb != "" {
			h = h.Add("heart-beat", hb)
		}
	}
	//

	return h
}
Exemplo n.º 2
0
// Common example TLS connect logic
func CommonTLSConnect(exampid, tag string, l *log.Logger,
	c *tls.Config) (net.Conn, *stompngo.Connection, error) {

	l.Printf("%stag:%s consess:%s common_tls_connect_starts\n",
		exampid, tag, Lcs)

	// Set up the connection.
	h, p := senv.HostAndPort()
	hap := net.JoinHostPort(h, p)
	n, e := net.Dial("tcp", hap)
	if e != nil {
		return nil, nil, e
	}

	c.ServerName = h // SNI

	nc := tls.Client(n, c) // Returns: *tls.Conn : implements net.Conn
	e = nc.Handshake()
	if e != nil {
		if e.Error() == "EOF" {
			l.Printf("%stag:%s consess:%s common_tls_handshake_EOF_Is_the_broker_port_TLS_enabled? port:%s\n",
				exampid, tag, Lcs,
				p)
		}
		l.Fatalf("%stag:%s consess:%s common_tls_handshake_failed error:%v\n",
			exampid, tag, Lcs,
			e.Error())
	}
	l.Printf("%stag:%s consess:%s common_tls_handshake_complete\n",
		exampid, tag, Lcs)

	l.Printf("%stag:%s connsess:%s common_tls_connect_host_and_port:%v\n",
		exampid, tag, Lcs,
		hap)

	// Create connect headers and connect to stompngo
	ch := ConnectHeaders()
	l.Printf("%stag:%s connsess:%s common_tls_connect_headers headers:%v\n",
		exampid, tag, Lcs,
		ch)
	conn, e := stompngo.Connect(nc, ch)
	if e != nil {
		return nil, nil, e
	}
	l.Printf("%stag:%s connsess:%s common_tls_connect_complete host:%s vhost:%s protocol:%s server:%s\n",
		exampid, tag, conn.Session(),
		h, senv.Vhost(), conn.Protocol(), ServerIdent(conn))

	// Show connect response
	l.Printf("%stag:%s connsess:%s common_tls_connect_response connresp:%v\n",
		exampid, tag, conn.Session(),
		conn.ConnectResponse)

	// Show heartbeat data (if heart beats are being used)
	if senv.Heartbeats() != "" {
		l.Printf("%stag:%s connsess:%s common_tls_connect_heart_beat_send hbsend:%v\n",
			exampid, tag, conn.Session(),
			conn.SendTickerInterval())
		l.Printf("%stag:%s connsess:%s common_tls_connect_heart_beat_recv hbrecv:%v\n",
			exampid, tag, conn.Session(),
			conn.ReceiveTickerInterval())
	}

	l.Printf("%stag:%s connsess:%s common_tls_connect_local_addr:%s\n",
		exampid, tag, conn.Session(),
		n.LocalAddr().String())
	l.Printf("%stag:%s connsess:%s common_tls_connect_remote_addr:%s\n",
		exampid, tag, conn.Session(),
		n.RemoteAddr().String())

	//
	return nc, conn, nil
}