forked from Azure/azure-service-bus-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace_websocket_example_test.go
58 lines (47 loc) · 1.31 KB
/
namespace_websocket_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package servicebus_test
import (
"context"
"fmt"
"os"
"time"
servicebus "github.com/Azure/azure-service-bus-go"
)
func ExampleNamespaceWithWebSocket() {
const queueName = "wssQueue"
connStr := os.Getenv("SERVICEBUS_CONNECTION_STRING")
if connStr == "" {
fmt.Println("FATAL: expected environment variable SERVICEBUS_CONNECTION_STRING not set")
return
}
// Create a Service Bus Namespace using a connection string over wss:// on port 443
ns, err := servicebus.NewNamespace(
servicebus.NamespaceWithConnectionString(connStr),
servicebus.NamespaceWithWebSocket(),
)
if err != nil {
fmt.Println(err)
return
}
// Create a context to limit how long we will try to send, then push the message over the wire.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
qm := ns.NewQueueManager()
if _, err := ensureQueue(ctx, qm, queueName); err != nil {
fmt.Println(err)
return
}
client, err := ns.NewQueue(queueName)
if err != nil {
fmt.Println(err)
return
}
// Send a message to the queue
if err := client.Send(ctx, servicebus.NewMessageFromString("Hello World!!!")); err != nil {
fmt.Println(err)
}
// Receive the message from the queue
if err := client.ReceiveOne(ctx, MessagePrinter{}); err != nil {
fmt.Println(err)
}
// Output: Hello World!!!
}