Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 44 additions & 58 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,32 @@ import (
"github.com/go-stomp/stomp/v3/frame"
)

func ExampleConn_Send(c *stomp.Conn) error {
func handle(_ error) {}

func ExampleConn_Send() {
c, err := stomp.Dial("tcp", "localhost:61613")
if err != nil {
handle(err)
}

// send with receipt and an optional header
err := c.Send(
err = c.Send(
"/queue/test-1", // destination
"text/plain", // content-type
[]byte("Message number 1"), // body
stomp.SendOpt.Receipt,
stomp.SendOpt.Header("expires", "2049-12-31 23:59:59"))
if err != nil {
return err
handle(err)
}

// send with no receipt and no optional headers
err = c.Send("/queue/test-2", "application/xml",
[]byte("<message>hello</message>"))
if err != nil {
return err
handle(err)
}

return nil
}

// Creates a new Header.
Expand All @@ -49,27 +55,6 @@ func ExampleNewHeader() {
doSomethingWith(h)
}

// Creates a STOMP frame.
func ExampleNewFrame() {
/*
Creates a STOMP frame that looks like the following:

CONNECT
login:scott
passcode:tiger
host:stompserver
accept-version:1.1,1.2

^@
*/
f := frame.New("CONNECT",
"login", "scott",
"passcode", "tiger",
"host", "stompserver",
"accept-version", "1.1,1.2")
doSomethingWith(f)
}

func doSomethingWith(f ...interface{}) {

}
Expand All @@ -78,78 +63,81 @@ func doAnotherThingWith(f interface{}, g interface{}) {

}

func ExampleConn_Subscribe_1() error {
func ExampleConn_Subscribe() {
conn, err := stomp.Dial("tcp", "localhost:61613")
if err != nil {
return err
handle(err)
}

sub, err := conn.Subscribe("/queue/test-2", stomp.AckClient)
if err != nil {
return err
handle(err)
}

// receive 5 messages and then quit
for i := 0; i < 5; i++ {
msg := <-sub.C
if msg.Err != nil {
return msg.Err
handle(msg.Err)
}

doSomethingWith(msg)

// acknowledge the message
err = conn.Ack(msg)
if err != nil {
return err
handle(err)
}
}

err = sub.Unsubscribe()
if err != nil {
return err
handle(err)
}

return conn.Disconnect()
conn.Disconnect()
}

// Example of creating subscriptions with various options.
func ExampleConn_Subscribe_2(c *stomp.Conn) error {
func ExampleConn_Subscribe_with_options() {
c, err := stomp.Dial("tcp", "localhost:61613")
if err != nil {
handle(err)
}

// Subscribe to queue with automatic acknowledgement
sub1, err := c.Subscribe("/queue/test-1", stomp.AckAuto)
if err != nil {
return err
handle(err)
}

// Subscribe to queue with client acknowledgement and a custom header value
sub2, err := c.Subscribe("/queue/test-2", stomp.AckClient,
stomp.SubscribeOpt.Header("x-custom-header", "some-value"))
if err != nil {
return err
handle(err)
}

doSomethingWith(sub1, sub2)

return nil
}

func ExampleTransaction() error {
func ExampleTransaction() {
conn, err := stomp.Dial("tcp", "localhost:61613")
if err != nil {
return err
handle(err)
}
defer conn.Disconnect()

sub, err := conn.Subscribe("/queue/test-2", stomp.AckClient)
if err != nil {
return err
handle(err)
}

// receive 5 messages and then quit
for i := 0; i < 5; i++ {
msg := <-sub.C
if msg.Err != nil {
return msg.Err
handle(msg.Err)
}

tx := conn.Begin()
Expand All @@ -162,81 +150,79 @@ func ExampleTransaction() error {
// acknowledge the message
err = tx.Ack(msg)
if err != nil {
return err
handle(err)
}

err = tx.Commit()
if err != nil {
return err
handle(err)
}
}

err = sub.Unsubscribe()
if err != nil {
return err
handle(err)
}

return nil
}

// Example of connecting to a STOMP server using an existing network connection.
func ExampleConnect() error {
func ExampleConnect() {
netConn, err := net.DialTimeout("tcp", "stomp.server.com:61613", 10*time.Second)
if err != nil {
return err
handle(err)
}

stompConn, err := stomp.Connect(netConn)
if err != nil {
return err
handle(err)
}

defer stompConn.Disconnect()

doSomethingWith(stompConn)
return nil
}

// Connect to a STOMP server using default options.
func ExampleDial_1() error {
func ExampleDial() {
conn, err := stomp.Dial("tcp", "192.168.1.1:61613")
if err != nil {
return err
handle(err)
}

err = conn.Send(
"/queue/test-1", // destination
"text/plain", // content-type
[]byte("Test message #1")) // body
if err != nil {
return err
handle(err)
}

return conn.Disconnect()
conn.Disconnect()
}

// Connect to a STOMP server that requires authentication. In addition,
// we are only prepared to use STOMP protocol version 1.1 or 1.2, and
// the virtual host is named "dragon". In this example the STOMP
// server also accepts a non-standard header called 'nonce'.
func ExampleDial_2() error {
func ExampleDial_with_options() {
conn, err := stomp.Dial("tcp", "192.168.1.1:61613",
stomp.ConnOpt.Login("scott", "leopard"),
stomp.ConnOpt.AcceptVersion(stomp.V11),
stomp.ConnOpt.AcceptVersion(stomp.V12),
stomp.ConnOpt.Host("dragon"),
stomp.ConnOpt.Header("nonce", "B256B26D320A"))
if err != nil {
return err
handle(err)
}

err = conn.Send(
"/queue/test-1", // destination
"text/plain", // content-type
[]byte("Test message #1")) // body
if err != nil {
return err
handle(err)
}

return conn.Disconnect()
conn.Disconnect()
}
Loading